iterating over an integer array in java

Associate
Joined
6 Jan 2005
Posts
722
Location
colchester,essex
Hi,

At the moment im just doing a bit on stacks and how to code them using a stack.

I create the int arraylist and then try to iterate over the collection using the standard code.

It doesnt compile however.

Is there something special i need to do to allow me to iterate over an int[]

cheers
 
public class IStackImpl implements IStack {

private int[] items;
private int topPointer;

public IStackImpl(int maxsz){
items = new int[maxsz];
topPointer = 0;
}

public String toString() {
Iterator it = items.iterator();
while(it.hasNext()){
System.out.println(it.next());
}

}

Thats the code basically, ive imported the arraylist and iterator classes. The line its having a prob with is

Iterator it = items.iterator();

It says cannot find symbol

please help
 
This is saying that to instantiate 'it', you should run a method (called iterator) on the 'items' object.

Iterator it = items.iterator();

I suggest that what you are trying to do is something like this:

Iterator it = iterator(items);

Unfortunately I think you might be slightly confused over what an interface is. An interface is a class that is used to define a common interface for similar processing for slightly different classes.
So you can have an "iterator" interface that ensures that has the same "placeholder" in code, but has different actual implementation. A class can only be a sub-type of a single type, but can implement more than one interfaces, making them slightly more useful as interfaces (if that makes sense).

jonc
 
mctrials23 said:
cheers for the answers, its just making a model of a stack using arrays and i need a toString method to list all the items in the stack and i wanted to iterate over the collection.

Just have an array, and an integer "pointer". The pointer points to the "top" of the stack. When you push onto the stack, increment the counter, and place the value there (start at zero). When popping from the stack, get the value at the pointer and decrement.

If the pointer gets larger than the size of the array, then throw an exception. If you try and pop off an empty stack, then throw an exception (be careful that you don' throw an exception when the integer is -1, as this represents an empty stack).
If you want to iterate, just copy the value of the pointer, and decrement it, each time getting the value at that position.


jonc
 
mctrials23 said:
public class IStackImpl implements IStack {

private int[] items;
private int topPointer;

public IStackImpl(int maxsz){
items = new int[maxsz];
topPointer = 0;
}

public String toString() {
Iterator it = items.iterator();
while(it.hasNext()){
System.out.println(it.next());
}

}

Thats the code basically, ive imported the arraylist and iterator classes. The line its having a prob with is

Iterator it = items.iterator();

It says cannot find symbol

please help


I may be wrong but last time I looked I didn't think you could use .iterator() on an array of that type? You might well need to use an ArrayList<Integer> instead?
Easier still (and definitely works) is just to use a for loop:

for(int i = 0 ; i< items.length(); i++)
{
System.out.println(items);
}

*code may not be quite accurate. .haven't done java for a while*
 
Iterators are for use with collections (lists, arraylists etc), and can't be used with a regular array. As has already been said the easiest way to print every item is with a loop, but need items.length not items.length(), as it is stored in a public variable, rather than having a method to find out.l
 
Dfhaii said:
Iterators are for use with collections (lists, arraylists etc), and can't be used with a regular array. As has already been said the easiest way to print every item is with a loop, but need items.length not items.length(), as it is stored in a public variable, rather than having a method to find out.l

Thought I might have got that wrong :-). I'm using C# now and wrote length down at first. . .then thought I was thinking about C# Properties which Java doesn't have!
 
Ah that's easy enough.

String myString = "";

for(int i = 0; i < items.length; i++){
Integer temp = new Integer(items);
myString += temp.toString();
}

Sommat like that should do the trick

EDIT: Error spotted.
 
Last edited:
What is the error you're getting?

Code:
public class test{
    private int[] beans;
    
    public test(){
        beans = new int[10];
        for (int i = 0; i < beans.length; i++){
            beans[i] = i;
        }
        String output = "";
        
        for (int i = 0; i < beans.length; i++){
            output += Integer.toString(beans[i]);
        }
        System.out.println(output);
    }
    
    public static void main(String[] args){
        test testy = new test();
    }
}

That compiles, runs and does what I think you're after?

Col
 
Last edited:
mctrials23 said:
still getting an error on the last line.

Its not liking it.

I suggest you get yourself a java book and read it, since you seem to be lacking the most basic skills / understanding . . No offence intended, but it really isn't helpful for you to try and program by 'guesswork'. . .You need to go through a book with examples in and get proper understanding of the language. Then you will be able to understand what to do and exactly what an error means (so you can correct it!).
 
iv sorted out the problem and i am working from a java book but its pretty poor tbh. Im at uni at the mo and am being taught java but the book leaves a lot of gaps.

Has anyone got any good recommendations.

The book i have at the moment is called "Objects first with java"
 
Objects first is a pretty good book to get you used to OO while using blueJ. O have an O'Reilly book called 'Learning Java' too but it's not great. I mainly used the Java API, and tutorials on the net whilst I was learning, however Java wasn't my first language so I don't know how much difference that made.
If you make sure you do, and understand all of the examples and exercises in the Objects First book you will get a pretty sound understanding of the syntax, language, and OO concepts.

Col
 
Back
Top Bottom