# iterator output not in sync

This topic is 1331 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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

}
}

}

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
Edited by run_g

##### Share on other sites
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.

##### Share on other sites
The problem is that each time you call next() you are stepping the iterator to the next element.

##### Share on other sites

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

##### Share on other sites
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);
}

##### Share on other sites

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.

##### Share on other sites

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(){

}
}


##### Share on other sites

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?

##### Share on other sites


Edited by run_g

##### Share on other sites
Does that even compile? The vector3D class you posted does not have a "next" method.

##### Share on other sites

No. this fix didnt compile, just Errors.

But the original code work except arraylist object didn't print out all data

I had the crash when i tried the code below.  So i adjusted it further to the posted and had errors

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


Edited by run_g

##### Share on other sites

Does that even compile? The vector3D class you posted does not have a "next" method.

*.next() and *.hasNext()  is part of the iterator class  ie     ListIterator<vector3D> vecIterator = vec.listIterator();

if you try to run all i posted originally you will see that the vector3D class doesn't have to have *.next() and *.hasNext()

i added  the code below becus it was suggested by @wooh..

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

Edited by run_g

##### Share on other sites

i added the code below becus it was suggested by @wooh..

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


This is the correct code, and should work. If this isn't working for you, you'll have to post that code so we can see why it isn't. We're not psychic, to help you, we'll need to see the full code, not just isolated snippets. If there are errors, please post them. If the output is unexpected, please note how and post it. Edited by rip-off

##### Share on other sites

This is the correct code, and should work. If this isn't working for you, you'll have to post that code so we can see why it isn't. We're not psychic, to help you, we'll need to see the full code, not just isolated snippets. If there are errors, please post them. If the output is unexpected, please note how and post it.

The whole code was posted in my 1st  and 5th posts.

Afterwards isolated snippets was because those were the only changes

But I will post all again here in one shot

Consist of the

class conceptPractice   which contains the main() function

class  myIterator

class  vector3D

output as in first post.                m  thanx


public class conceptPractice {

static myIterator scanArray;

public static void main( String[] args ){

scanArray = new myIterator();
scanArray.createListVector3D();
}
}

import java.util.ArrayList;
//import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

public class myIterator{

List<vector3D> vec = new ArrayList<vector3D>();
vector3D  myTwoDimVec[][] = new vector3D[3][5];

myIterator( ){

for(int i=0; i<myTwoDimVec.length; i++){
for(int j=0; j<myTwoDimVec[i].length; j++){

//but this code makes it 10, 20, 30 11, 21, 31 12, 22, 32 etc
// comment as in first post

myTwoDimVec[i][j] = new vector3D(10+(i*myTwoDimVec[i].length+j), 20+(i*myTwoDimVec[i].length+j), 30+(i*myTwoDimVec[i].length+j));

}
}
}

public void createListVector3D(){

for(int i=0; i<myTwoDimVec.length; i++){
for(int j=0; j<myTwoDimVec[i].length; j++){

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()){
System.out.println( "iterator list output   " + vecIterator.next().x +  "   " + vecIterator.next().y  +  "   " + vecIterator.next().z  );
}

}
}

//=============================== vector3D class ==================================================

import java.io.*;
import java.math.*;

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(){

}
}

Edited by run_g

##### Share on other sites
So, why are you not adding the actual fix as Wooh and especially rip-off already told you? Repeatedly. If that fix does not work, do as rip-off told you: post the errors (either the compile-time errors or the new behavior).

##### Share on other sites

So, why are you not adding the actual fix as Wooh and especially rip-off already told you? Repeatedly. If that fix does not work, do as rip-off told you: post the errors (either the compile-time errors or the new behavior).

I did exactly that.

Perhaps you are so much in a hurry to dismiss me that you didnt read the posts properly or follow the logic properly. (with respect : I don't mean to sound awful)

I know u know the following already but to reiterate I will add this anyway - Eclipse IDE doesn't work like visual studio. U see red error lines. not compile errors.

Put the cursor on it and you see the errors. I immediately knew that vector3D v = vecIterator.next(); wouldn't work because the return type doesn't match vector3D data type

so   System.out.println("iterator list output " + v.x + " " + v.y + " " + v.z);   also had red lines. No compile errors (or it the editor automatically compiles without printing to the console)

All i was asking was a fresh eyes to look at it but you all kept refering to Wooh's fix which can never be the correct fix

Edited by run_g

##### Share on other sites

EDIT:

You might also have to put the next thing after the  print, instead of before. Depends on the behaviour on start/end.

Even though Wooh's post might not be a 100% copy/pastable fix, it still highlights where the problem is, and the broad logic for fixing it. This is especially true for his follow-up post.

----

ORIGINAL:

That's still where the problem is. I don't really know the syntax, but I'd guess something like this?

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

Edited by Lactose!

##### Share on other sites

I know u know the following already but to reiterate I will add this anyway - Eclipse IDE doesn't work like visual studio. U see red error lines. not compile errors.

That is bullshit. An IDE which does not show actual errors is completely useless for its purpose. The last time I used Eclipse you needed to manually save the file (for example Ctrl-S) to trigger a refresh of the error list. If that does not work for you, I would suggest you immediately drop everything else and find out how to get a list of errors nowadays or switch IDEs. It will be impossible to get anything done beyond the simplest of things without quick and easy access to the actual errors.

Edited by BitMaster

##### Share on other sites

I immediately knew that vector3D v = vecIterator.next(); wouldn't work because the return type doesn't match vector3D data type

This is not true.  It does match, how else do you think you can access the public members of the vector3D class from vecIterator.next()?

Wooh's post should work exactly as is.  I would suggest you try to run it.

you all kept refering to Wooh's fix which can never be the correct fix

Not only is it the correct fix, but had it not been he even explained to you why you should expect to see the behavior you do when running your program.  And how you could get the behavior you require.

Edited by DeafManNoEars

##### Share on other sites

I do apologise

Wooh, rip-off, DeafManNoEars, BitMaster, Lasctose  et al

It did work with the correct behaviour with Wooh exact code

I dont know what mistakes i made originally that caused the errors that i observed the first times i tried it, I must overwritten some other code

My apologies and many thanks

Edited by run_g

##### Share on other sites
Glad it is working for you now. Just to point out that this is why I was urging you to post the actual, full code that you thought you had corrected that still wasn't working. There was probably some minor mistake in your initial attempt that we may have caught earlier.