Solved

Element in arraylist of arraylists

Posted on 2013-11-19
2
283 Views
Last Modified: 2013-11-19
Working in Java, how do I address a specific item in an array list of arraylists?

I have the constructor:
ArrayList<ArrayList<Integer>> testpaths = new ArrayList<ArrayList<Integer>>();

Which when I print to console:
System.out.println(testpaths + "\n");

Output after running some method() is:
[[], [1], [2], [1, 5, 3], [2, 4], [1, 5], [1, 5, 6], [1, 5, 7]]

Let's say that in that 8th item [1,5,7] - I just want the "1".

Thanks!
0
Comment
Question by:Geisrud
  • 2
2 Comments
 
LVL 14

Accepted Solution

by:
jb1dev earned 500 total points
ID: 39661102
testpaths.get(7).get(0);
0
 
LVL 14

Expert Comment

by:jb1dev
ID: 39661112
The above should work fine provided you have the generics in place, as you do in your example. If you did not have the generics in place, you would need to cast the first get() result back as an ArrayList.

E.g.
((ArrayList)testpaths.get(7)).get(0);
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Navigation is an important part of web design from a usability perspective. But it is often a pain when it comes to a developer’s perspective. By navigation, it often means menuing. This is less theory and more practical of how to get a specific gro…
If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
The viewer will learn how to implement Singleton Design Pattern in Java.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

840 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