Solved

list iterator

Posted on 2008-06-23
6
163 Views
Last Modified: 2010-03-30
Hi,

I use iterators with maps pretty often like:

    for (Map.Entry<Integer, String> it: mylist.entrySet()) {
       it.getValue().toString();
    }

how do you do the same with an ArrayList?

Thanks
0
Comment
Question by:DJ_AM_Juicebox
  • 2
  • 2
  • 2
6 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 250 total points
ID: 21850253
for (T t : someList)

(where 'T' is the type)
0
 
LVL 92

Expert Comment

by:objects
ID: 21851250
Heres an example:

ArrayList<String> list = new ArrayList<String>();

for (String s : list}
{
    System.out.println(s);
}

0
 

Author Comment

by:DJ_AM_Juicebox
ID: 21862528
Quick question on that, consider:

public class Test
{
    ArrayList<Car> m_cars;

    public ArrayList<Car> getCars()
    {
        return m_cars;
    }
}

public static void main()
{
    Test test = new Test();

    for (Car car : test.getCars())
    {
         ....
    }
}

every time an iteration of the for-loop is done, does it need to call getCars() for any reason? I'm comparing that to:

    for (int i = 0; i < test.getCars().size(); i++) {
          ...
    }

where it would have to call getCars() every iteration.

Thanks
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 92

Assisted Solution

by:objects
objects earned 250 total points
ID: 21862560
no it wouldn't (it would use iterator)

>     public ArrayList<Car> getCars()

on a side note, making it return List instead of ArrayList is generally abetter idea.
Allows you to change the List implementation used in your class in the future without affecting users of the class.

    public List<Car> getCars()

0
 

Author Comment

by:DJ_AM_Juicebox
ID: 21862574
ok thanks
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 21864147
:-)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now