Solved

list iterator

Posted on 2008-06-23
6
170 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

632 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