Solved

Element in arraylist of arraylists

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
difference of if loops 23 49
fibonacci ten numbers 4 38
servlet example 11 40
by zero exception 10 39
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…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

776 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