Advertisement

iterator output not in sync

Started by July 01, 2014 11:20 PM
20 comments, last by rip-off 10 years, 6 months ago

my iterator output (below) is picking up diagonals of vector3D array, why? how can i fix this code so the output is the same as the 2-d array output (below also)?

Or is it a fact that iterators cannot be used with 2d arrays

many thanks (just edited to add some comments now. Hope that helps)


public class myIterator{
	
	List<vector3D> vec = new ArrayList<vector3D>();
	vector3D  myTwoDimVec[][] = new vector3D[3][5];
	
	myIterator( ){
		
		for(int i=0; i<myTwoDimVec.length; i++){      //double loop to traverse 2d vector array
			for(int j=0; j<myTwoDimVec[i].length; j++){
          
                              //setting values the x,y,z data feild of the vector3D 
                              // in order to make each data unique value and thus debugging is tractable
                              // i am incrementing the data by 1 using i * myTwoDimVec[i].length + j
                              //                   this is         row* MaxColumnLength + current column
                              // in other words if i do not increase by 1 at all then it will be
                              //myTwoDimVec[i][j] = new vector3D(10, 20, 30 );  and all the data will look the same
                              //but this code makes it 10, 20, 30    11, 21, 31    12, 22, 32 etc    
                              // so with this difference i can track which data went missing
                                 
				myTwoDimVec[i][j] = new vector3D(10+(i*myTwoDimVec[i].length+j), 20+(i*myTwoDimVec[i].length+j), 30+(i*myTwoDimVec[i].length+j));

				vec.add( myTwoDimVec[i][j] );            // adding to the arraylist

			}
		}
		
	}
	
	public void createListVector3D(){
		
		for(int i=0; i<myTwoDimVec.length; i++){           //double loop to print out the 
			for(int j=0; j<myTwoDimVec[i].length; j++){// data directly- not using *.hasNext
				
				System.out.println("2 dimen array output   "+myTwoDimVec[i][j].x + "   " + myTwoDimVec[i][j].y + "   " + myTwoDimVec[i][j].z );
			}
		}
		
		ListIterator<vector3D>  vecIterator = vec.listIterator();

		System.out.println( "=============================================================");

		while( vecIterator.hasNext()){   // outputing using hasNext
			System.out.println( "iterator list output   " + vecIterator.next().x +  "   " + vecIterator.next().y  +  "   " + vecIterator.next().z  );
		}
		

	}
	
}

public class conceptPractice {
	
	static myIterator scanArray;
	
	public static void main( String[] args ){
		
		scanArray = new myIterator();
		scanArray.createListVector3D();
	}
}

2 dimen array output 10.0 20.0 30.0
2 dimen array output 11.0 21.0 31.0
2 dimen array output 12.0 22.0 32.0
2 dimen array output 13.0 23.0 33.0
2 dimen array output 14.0 24.0 34.0
2 dimen array output 15.0 25.0 35.0
2 dimen array output 16.0 26.0 36.0
2 dimen array output 17.0 27.0 37.0
2 dimen array output 18.0 28.0 38.0
2 dimen array output 19.0 29.0 39.0
2 dimen array output 20.0 30.0 40.0
2 dimen array output 21.0 31.0 41.0
2 dimen array output 22.0 32.0 42.0
2 dimen array output 23.0 33.0 43.0
2 dimen array output 24.0 34.0 44.0
=============================================================
iterator list output 10.0 21.0 32.0
iterator list output 13.0 24.0 35.0
iterator list output 16.0 27.0 38.0
iterator list output 19.0 30.0 41.0
iterator list output 22.0 33.0 44.0
It's a bit hard to read (for example, why do you use camelCase instead of CamelCase for class names?) but from what I understand you never use an Iterator to output anything from the 2d-array (your 2d output is handrolled, your iterator output traverses a List, which is a 1d structure). Obviously you could write an implementation of Iterator which iterates over a 2d-array (although there would be several ways to do that).

To be honest, I'm not sure what you are doing, what you expect to do and what you see happening. A few more explanations would allow people to help you.
Advertisement
The problem is that each time you call next() you are stepping the iterator to the next element.


To be honest, I'm not sure what you are doing, what you expect to do and what you see happening. A few more explanations would allow people to help you

Just trying to add some vector3d data with x, y, z to an array list, and then output the data back using *.hasNext(). To show if the output is right or not i am also comparing with printing out the 2d vector3D out directly.

So on comparing the output i found that *.hasnext() misses out on a whole lot of data

I will edit and add some comments to the code

Added some comments, hope that help understanding the code :)

Not sure if you missed my reply or maybe I was not clear.

Each time you call next() you will get a different element. In your code you are printing the x of the first element, the y of the second element and the z of the third element. In the next iteration you print the x of the fourth element, the y of the fifth element and the z of the sixth element. And so on.

To print the x, y and z of the same element you should only call next() once in each iteration.

while (vecIterator.hasNext()) {
	vector3D v = vecIterator.next();
	System.out.println("iterator list output " + v.x + " " + v.y  + " " + v.z);
}
Advertisement


To print the x, y and z of the same element you should only call next() once in each iteration.

while (vecIterator.hasNext()) {
vector3D v = vecIterator.next();
System.out.println("iterator list output " + v.x + " " + v.y + " " + v.z);
}

when I adjusted the code as you suggested i had an exception crash, i guess you are must be right but i not connecting with your suggestion properly.


To print the x, y and z of the same element you should only call next() once in each iteration.

while (vecIterator.hasNext()) {
vector3D v = vecIterator.next();
System.out.println("iterator list output " + v.x + " " + v.y + " " + v.z);
}

Here is the remaining part of the code (vector3D class), you can check the fix/adjustment also , thanks


public class vector3D{ 
	float x;
	float y;
	float z;
	
	vector3D(){	
	}
	
	vector3D(float a, float b, float c){
		x = a;
		y = b;
		z = c;
	}
	
	public void sum(){
		
	}
}

when I adjusted the code as you suggested i had an exception crash, i guess you are must be right but i not connecting with your suggestion properly.

Can you show us the adjusted code?


This topic is closed to new replies.

Advertisement