Solved

Element in arraylist of arraylists

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
ArrayLists in ArrayLists/HashMaps? 4 39
wordappend challenge 8 84
Modeling a class in java 5 31
Copy Files - Python 7 55
There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

707 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

17 Experts available now in Live!

Get 1:1 Help Now