Question:
using index counters in java?
Starlight*_*
2009-11-03 05:55:39 UTC
i have to do this exact program, just change it to index counter instead of iterator....any ideas, thanks
.....................
import java.util.*;
class Iterator2{
public static void main(String[] args){
ArrayList store = new ArrayList();
int i;

for(i = 0; i < 10; i++){
int numbers = (i+1);
store.add(numbers);
}
Iterator MyIterator = store.iterator();
while (MyIterator.hasNext()){
int value = MyIterator.next();
System.out.println(value);
}
System.out.println ("The Array List Contains " + i + " items.");
}
}
Three answers:
Sam
2009-11-03 06:06:12 UTC
1. You don't need to use Iterator to get size of an ArrayList, instead use size(). i.e.



ArrayList store = new ArrayList();

//do some stuff

System.out.println("The ArrayList contain " + store.size() + "items.");



2. If you want to go through an ArrayList you can just use a simple for loop. i.e.



for(int i = 0; i < store.size(); i++)

{

System.out.println(store.get(i));

//or whatever you wanna do

}
?
2016-12-19 02:13:03 UTC
not in user-friendly words the mattress, the closet too. ;-; If I dropped easily considered one of my crammed animals off of the mattress on a similar time as i replaced into attempting to sleep, and the room replaced into dark, i ought to enable it stay there by capacity of fact i presumed some ingredient ought to snatch my arm from below the mattress as i attempted to %. it up. :3 i don't think of that stuff anymore however. I however get spooked by capacity of utilising the closet if I watch an somewhat frightening horror action image however. >->
deonejuan
2009-11-03 06:08:54 UTC
I would say...

counter = 0;

while( counter < store.size() ) {

System.out.printlin( store.get( counter++ ) );

}



// because you fetch from ArrayList by .get( index );


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...