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
Does that even compile? The vector3D class you posted does not have a "next" method.

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

Advertisement

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

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.

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 smile.png

But I will post all again here in one shot biggrin.png

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

				vec.add( myTwoDimVec[i][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(){
		
	}
}
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).
Advertisement

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 respectsmile.png : 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

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

Hello to all my stalkers.

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.


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.

This topic is closed to new replies.

Advertisement