Is there any performance testing results available in comparing traditional for loop vs Iterator while traversing a ArrayList,HashMap and other collections?
Or simply why should I use Iterator over for loop or ... |
I have an ArrayList and I am using an iterator to run through it. I need to find out what type of object is next:
Iterator vehicleIterator = vehicleArrayList.iterator();
while(vehicleIterator.hasNext())
{
...
|
ArrayList array = new ArrayList();
Iterator it1 = array.iterator();
while (it1.hasNext()){
Myclass temp = it1.myGetterMethod();
System.out.println (temp);
}
This is what I would like to implement, but Iterator only returns a ... |
How would you explain to someone who has just started programming in Java, what the difference between ArrayLists and Iterators are?
Why I would use an iterator instead of using the get() ... |
Ok so I have an ArrayList (arrBok), which is full of book objects (the code is in Norwegian, so pay no attention to that please). I want to make a public ... |
I have to iterate through an arraylist in this manner.
ArrayList<Integer> li = new ArrayList<Integer>();
li.add(20);
li.add(30);
li.add(40);
li.add(50);
li.add(70);
for (int i = 0; i < li.size() - 1; i++)
System.out.println(li.get(i) + " " ...
|
If I set up an iterator for myList:
Iterator iter = myList.iterator();
while(iter.hasNext())
{
MyObj myObj = (MyObj)iter.next();
doPrint(myObj.toString());
}
And I call it a second time:
while(iter.hasNext())
{
...
|
|
Scenario : An Arraylist containing following DTOs'
MetadatRetrievalDTO[] dto = new MetadatRetrievalDTO[16];
dto[0] = new MetadatRetrievalDTO();
dto[0].setArticleId(11);
dto[0].setBaseId("SB11");
...
|
Maybe I am missing something but I thought that If I declare my class as such:
public class Something<T> implements Iterable<Iterable<T>> {
public Something(Iterable<Iterable<T>> input) {
...
I should be able ... |
I have 2 classes Student class and StudentTest class. In my Student class I need to write a method called remove(int ID) to remove a student with a specific ID, from ... |
I defined an interface StringStack
public interface StringStack{
//add a value to the stack
public void push(String value);
//fetch top-most element of the ...
|
What I am trying to do is to loop through a list of indices that refer to a paragraph somewhere. If there paragraph is too short, remove the index from the ... |
Edit: Thank you for all your prompt replies. Now I see the assignment won't work. From another thread I read that iterator in Java is much less powerful than iterator in ... |
ArrayList's list iterator does implement the remove method, however I get the following exception thrown:
UnsupportedOperationException at java.util.AbstractList.remove(AbstractList.java:144)
By this code:
protected void removeZeroLengthStringsFrom(List<String> stringList)
{
ListIterator<String> iter = stringList.listIterator();
...
|
So right now I have a program containing a piece of code that looks like this...
while (arrayList.iterator().hasNext())
{
if( arrayList.iterator().next().equals(value)) //value is equal to a String value
...
|
so I was wondering if anyone knew how I would go about reading in mutliple values from a key, delimiting them by commas and storing them to an arraylist from a ... |
I have a ArrayListof some POJO. Let say Employee.
Then I am getting another ArrayList which contains only 'id' of that POJO.(i.e. Employee)
Now, I want a sub list of matching ids from ... |
I had a problem with adding elements to my ArrayList while using an Iterator. In the following code it gives me this output:
a
k
s
But still it misses the one I had added ... |
I have question here. I have two list and both this list have some common elements.
This common elements along with the values has to be put in another List. which is ... |
Hi, With a little help I've finally managed to add an arrayList to my swing program. The problem is now with iteration through this list. When I try to go through the list, it simply jumps straight to the beginning or end of the list, depending on whether I click the backwards or forwards button. The arrayList is printing out correctly, ... |
You would use a Collection to allow you room to change which implementation you wanted to use. Suppost originally all you needed was something to hold a list of objects, well an ArrayList will do fine. But then you realise that the values of the objects in this collection must be unique so you change it to a Set. If you ... |
hi Team i want to know that can i give an iterator to an arraylist, that starts from a dynamic location in the arraylist. In my application, there is one arraylist, that contains a number of objects. i want to take each object and compare that object with the remaining objects in the arraylist. for example, take the first object, compare ... |
If you open the rt.jar file with WinZip or FileLibrarian or whatever you use, just extract the class files that you want and put them with your class files. Be carefull though, because you need to research to take all the other classes that those 2 rely on to be sure that it works. |
ok, so that makes more sense. If I don't go to the next element, it will stay there forever, but by calling next() it actually brings me to the next element. It was looping before, because it was never making it to the next element. Thanks again. [ July 07, 2005: Message edited by: Nicholas Carrier ] |
|
|
Hi I have one query , I have seen in the java doc , ArrayList inherited iterator() from its super class java.util.AbstractList and from interface java.util.List. I am not able to understand why it is inheriting same method from different place ? Could ny body explain me what is the reason ? regards, S |
Good evening, can I ask for some guidance with an issue I have with a small program? I am having problems with a couple of classes using an ArrayList and an Iterator with a scanner reading in a file. When the file has been read in and another class attempts to return the elements in the Iterator, it only reports back ... |
Hi there, Please refer following code snippet. 1 List arrayList = new ArrayList(); 2 arrayList.add(new Integer(24)); 3 arrayList.add(new Double(2)); 4 arrayList.add(new Employee( Employee.getNewEmployeeId(), "Manager"); 5 ListIterator iterator = arrayList.listIterator(); 6 arrayList.add(new Employee(Employee.getNewEmployeeId(), "Engineer"); This code throws java.util.ConcurrentModificationException as i am trying to add Object after assigning iterator to the List. I found that we have to use add method of iterator, ... |
|
craq wrote: bingo :-D thanks welcome. just for interest, what's the difference between awt and util? (Or where could I read about that?) I'm used to netBeans sorting that stuff out for me. It's time to leave the NetBeans womb and venture out into the wild world; this includes having a look at the API and the Sun tutorials. My recommendation ... |
Thanks for the response, i'll give the suggestions you made a try. I'm not iterating child twice in the code, at least i didn't think I was! What I was trying to do was take the arraylist of children from the person and iterate through that list so that all of those children would be added to the descendants list. Once ... |
Hi all, I got a couple questions regarding ArrayList, Iterator, and objects. How would you store the results of an SQL result set in an ArrayList using the setters and getters from an object (MyItems). This MyItems class has attributes that correspond directly to the items in the database (title, ISBN, author, etc). Basically, I need take the results from an ... |
|
Are you asking whether it's faster to use an iterator or get(index) in a for loop? Or are you asking whether it's faster to use an iterator or get(index) to get a single value? If the former...then just use the iterator. Even if one could demonstrate a speed improvement with get(index) (which seems unlikely) it's not worth it because you're making ... |
ArrayList arrayList = new ArrayList(); arrayList.add(new Rectangle()); arrayList.add(new Rectangle(22, 57)); arrayList.add(new Circle()); arrayList.add(new Circle(11)); HashMap map = new HashMap(); Iterator iter = arrayList.iterator(); int count = 0; while (iter.hasNext()) { String key = "ID_" + ++count; Object value = iter.next(); map.put(key, value); System.out.println("(Added " + key + ") " + value); } BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); while (true) { ... |