Solved

Element in arraylist of arraylists

Posted on 2013-11-19
2
279 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

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

932 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

11 Experts available now in Live!

Get 1:1 Help Now