I have to use 10 ArrayLists for a project, instead of creating 10 separate arraylists. I was told to use: ArrayList<ArrayList<Integer>> lists = new ArrayList<>(); and to create the 10 arrays to use: for(int i=0;i<10;i++){ lists.add(new ArrayList<Integer> ());
I have never studied arraylists inside of a arraylist, so this is very new to me. How do I access the arrays inside of the array? They do not have a name that I can see to access, so how do I call each individual arraylist?
You can create an arraylist of arraylists as shown in the code below. If you want to access all the inner array lists just use a for each loop. If you want a specific list (array list is ordered) just use the index (both ways are shown in the example below):
ArrayList<ArrayList<String>> listOfLists = new ArrayList<ArrayList<String>>();
ArrayList<String> firstList = new ArrayList<String>() {{
add("A");
add("B");
add("C");
}};
ArrayList<String> secondList = new ArrayList<String>() {{
add("D");
add("E");
add("F");
}};
listOfLists.add(firstList);
listOfLists.add(secondList);
//access all inner lists
for(ArrayList<String> innerList:listOfLists) {
System.out.println("INNER LIST --> "+innerList);
}
//access specific list
System.out.println(listOfLists.get(1));
To add a number to a list in the list of lists, you have to get it using its index, then you can add to it.
lists.get(0).add(42);
Let say you wanna access some index of a particular ArrayList inside the ArrayList of ArrayList
e.g.
arrayListOfarrayList.get(indexOfArrayList).get(indexOfElement);
If you wanna add something
arrayListOfarrayList.get(indexOfArrayList).add(yourElement);
You can refer to ArrayList API docs for more details
https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html
Related
If i have an arraylist, and i want both a sorted and unsorted version of it, how can i achive it.
tried with Collections
ArrayList<Integer> someNumbers = new ArrayList<>();
if i then make a new arraylist = someNumbers and sort it. The orginal one gets sorted too ?
ArrayList<Integer> sortedNumbers = someNumbers;
Collections.sort(sortedNumbers);
both list gets sorted.
How can i simply achive what im trying to do ? ( get a sorted copy, and keep the orginal in its orginal ordering )
When you do ArrayList<Integer> sortedNumbers = someNumbers;, you are not creating a new list to sort. You are basically assigning the variable sortedNumbers to the exact same list that someNumbers is assigned to.
To fix it, you can create a new list from your other list. Like this:
ArrayList<Integer> sortedNumbers = new ArrayList<>(someNumbers);
The reason why both lists get sorted is because even you first create a new ArrayList(), you then override this instance with a reference to someNumbers. Therefore when you sort sortedNumbers you actually sort the original list someNumbers.
There are several ways you can achieve your goal:
ArrayList<Integer> sortedNumbers = new ArrayList<>();
sortedNumbers.addAll(someNumbers);
Or you can achieve the same in one step by using the copy constructor:
ArrayList<Integer> sortedNumbers = new ArrayList<>(someNumbers);
You need to make a copy of the list and then sort it, e.g.
ArrayList<Integer> sortedNumbers = new ArrayList<>(someNumbers);
Collections.sort(sortedNumbers);
This uses ArrayList(java.util.Collection) constructor
Just copy the array and sort the copy:
ArrayList<Integer> sortedNumbers = new ArrayList<>(someNumbers);
Collections.sort(sortedNumbers);
You have to copy the ArrayList first to have two versions. Then you can sort one of the two ArrayLists
ArrayList<Integer> someNumbers = ...
ArrayList<Integer> sortedNumbers = new ArrayList<>(someNumbers);
Collections.sort(sortedNumbers);
I want to add COPIES of data to my List but when I use .add, it adds a reference and not a copy. I'll try to explain what I mean.
List<List<String>> formattedTempMatches = new ArrayList<>();
ArrayList<String> rowFormattedMatches = new ArrayList<>();
rowFormattedMatches.add(matchesArray[0]);
rowFormattedMatches.add(matchesArray[1]);
rowFormattedMatches.add(matchesArray[2]);
formattedTempMatches.add(rowFormattedMatches);
//rowFormattedMatches.clear();
rowFormattedMatches.add(matchesArray[3]);
rowFormattedMatches.add(matchesArray[4]);
rowFormattedMatches.add(matchesArray[5]);
formattedTempMatches.add(rowFormattedMatches);
I've written my code outside of a loop to try to explain myself better. I want to add 3 elements to an ArrayList (of which the elements come from a normal array) then add that ArrayList to a list of lists. When the ArrayList is added to the list, I want to clear it and refill it with 3 more elements and then add it to the next index of the List. The problem is once I clear it, the data is removed from the list. If I don't clear it, the list has 6 elements at each index when there's only supposed to be 3. What should I do?
Apologies for my possibly confusing explanation.
The call of clear() empties the list. As you are using the same instance for each iteration, this will not work. What you can do instead of clearing the list is create a new instance:
List<List<String>> formattedTempMatches = new ArrayList<>();
ArrayList<String> rowFormattedMatches = new ArrayList<>();
rowFormattedMatches.add(matchesArray[0]);
rowFormattedMatches.add(matchesArray[1]);
rowFormattedMatches.add(matchesArray[2]);
formattedTempMatches.add(rowFormattedMatches);
rowFormattedMatches = new ArrayList<>(); // new instance of an empty list
rowFormattedMatches.add(matchesArray[3]);
rowFormattedMatches.add(matchesArray[4]);
rowFormattedMatches.add(matchesArray[5]);
formattedTempMatches.add(rowFormattedMatches);
I have an arraylist of an arraylist declared like
ArrayList<ArrayList<Integer>> bigList = new ArrayList<ArrayList<Integer>>();
I would then add to this bigList by bigList.add(arraylist).
I then have a class that takes ArrayList<Integer> as a construcor parameter. My question is how do I send a certain ArrayList in bigList to this class as a constructor parameter? I can iterate through my bigList with
for(ArrayList<Integer> list : bigList) {
for(Integer num : list)
System.out.println(num);
}
but I have not been able to send a whole ArrayList element to another class. Thanks a bunch.
You can select an ArrayList at a particular index and pass it to a constructor as follows:
int index = 2; // the index of the list you want to pass to the constructor
MyNewObject newObject = new MyNewObject(bigList.get(index));
Have you tried:
List<SomeClass> sList = new ArrayList<>();
for(ArrayList<Integer> list : bigList) {
sList.add(new SomeClass(list));
}
This would work fine if you are looking to iterate though all lists in bigList and construct new instances from all of them. Alternatively, if you just want to create one such object for a specific list at index "i" of bigList:
SomeClass s = new SomeClass(bigList.get(i));
I'm working with Depth First Search program and I'm trying to create a Adjacency List Representation.
I read through some articles stating that an creating ArrayLists within an ArrayList would be the best representation.
Let's say I initialized the arraylist within a arraylist like so:
List<List<Integer>> adjList = new ArrayList<List<Integer>>();
My question is how would you input data into the arraylist MANUALLY. I'm trying to understand the concept of arraylist with an arraylist before I begin my programming. If someone could possibly insert data into this arraylist so I could see the proper way of setting up.
Any additional input on anything I might need or take in consideration is recommended.
BTW: This is not a homework assignment just using personal time looking through my old textbooks.
Let's say you want to add 2 lists, one with 1 and 2 and the other with 10 and 20. A very manual way of adding could be:
List<List<Integer>> adjList = new ArrayList<List<Integer>>();
adjList.add(new ArrayList<Integer>()); // initialise new ArrayList<Integer>
adjList.get(0).add(1); // add value one by one
adjList.get(0).add(2);
adjList.add(new ArrayList<Integer>());
adjList.get(1).add(10);
adjList.get(1).add(20);
You could also write it this way:
List<List<Integer>> adjList = new ArrayList<List<Integer>>();
ArrayList<Integer> a1 = new ArrayList<Integer>(); // initialise new ArrayList<Integer>
a1.add(1); // add value one by one
a1.add(2);
adjList.add(a1);
ArrayList<Integer> a2 = new ArrayList<Integer>(); // initialise new ArrayList<Integer>
a2.add(10); // add value one by one
a2.add(20);
adjList.add(a2);
Well, a list of a list of Integer objects could be done as such:
List<List<Integer>> adjList = new ArrayList<List<Integer>>();
List<Integer> li = new ArrayList<Integer>();
for (int i = 0; i < 10; i++) {
li.add(i);
}
adjList.add(li);
Add to each sublist, and then add the sublist.
The adjList can contain the elements of the type List<Integer>, so create one and add using add(E element) function as we would for adding an element:
ArrayList<Integer>aList = new ArrayList<>();
adjList.add(aList);
Then to add an element to the element(which has the type List<Integer>) of adjList: you can try getting it using get(index) and add your element:
adjList.get(0).add(10);
adjList.get(0).add(22);
Try adding a second list and get it's index using get(1) and add the Integer element to the list at index 1 as the above example suggest. There are other known function too. Please check the class ArrayList<E> documentation page.
This will help
public static void main(String[] args){
//creating a new ArrayList of List of Integers
ArrayList<List<Integer>> integerListContainer = new ArrayList<List<Integer>>();
//Creating the first child arraylist of Integers
ArrayList<Integer> firstChildintegerList = new ArrayList<Integer>();
//filling the values 1,2,3 in it
firstChildintegerList.add(1);
firstChildintegerList.add(2);
firstChildintegerList.add(3);
//adding this integer list to the parent list
integerListContainer.add(firstChildintegerList);
//Creating the second child arraylist of Integers
ArrayList<Integer> secondChildintegerList = new ArrayList<Integer>();
//filling the values 10,20,30 in it
secondChildintegerList.add(10);
secondChildintegerList.add(20);
secondChildintegerList.add(30);
//adding this integer list to the parent list
integerListContainer.add(secondChildintegerList);
System.out.println("Printing the parent list to see what it has: ");
System.out.println(integerListContainer.toString());
}
Hope it clearly explains what happens
I'm very new to Java programming and I'm trying to take elements from a list and put them into a superlist as singleton lists:
[object1,object2,object3] ---> [[object1][object2][object3]]
How do I do this?
If you want to create a list of lists, you can do something like:
List<List<Object>> list = new ArrayList<List<Object>>();
Where Object is the type of object you want to put in your list, for example a String object, then you'd just write:
List<List<String>> list = new ArrayList<List<String>>();
If you wonder "How can I access the elements from the list inside the main list", then first you know to access the list inside your main list like:
List list2 = list.get(index);
Next you can use list2 to access the elements of the list inside your main list like:
Object o = list2.get(index);
etc