Solved

list iterator

Posted on 2008-06-23
6
169 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

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

734 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