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();
}
}