I have added an item to list a and then added list a to list b and did the same thing again.
My question is if I print b.get(0) and b.get(1), I am getting the same list that is both the items "One" and "Two", why is it so?
At b.get(0) I want to get only one item I added that is a.add("One").
After adding a.add("Two"), if I print b.get(1) I should get both "One" and "Two"?
Is there any solution or any changes to manage this?
List<String> a= new ArrayList<String>();
List<List<String>> b= new ArrayList<List<String>>();
a.add("One");
b.add(a);
a.add("Two");
b.add(a);
System.out.println("b="+b.get(0));
System.out.println("b="+b.get(1));
output:
b=[One, Two]
b=[One, Two]
You are adding the same List twice, so you see the same elements for both indices of the outer List.
In order to add two different Lists, you must create a new ArrayList before adding each element to the outer List :
a.add("One");
b.add(a);
a = new ArrayList<>(a); // assuming you want the second list to contain both "One" and "Two"
a.add("Two");
b.add(a);
You are adding the same reference in b[0] and b[1]. If you want to have differente lists at diferent index on list b you have to create a new List object
List<String> a= new ArrayList<String>();
List<String> c= new ArrayList<String>();
List<List<String>> b= new ArrayList<List<String>>();
a.add("One");
b.add(a);
c= new ArrayList<String>();
c.addAll(a);
c.add("Two");
b.add(c);
System.out.println("b="+b.get(0));
System.out.println("b="+b.get(1));
the reason is in your code, the b.get(0) and b.get(1) point to the same List a, so the output is same.
use this code can achieve what you want,
List a1= new ArrayList();
List a2= new ArrayList();
List> b= new ArrayList>();
a1.add("One");
b.add(a1);
a2.add("Two");
b.add(a2);
System.out.println("b="+b.get(0));
System.out.println("b="+b.get(1));
output is,
b=[One]
100
b=[Two]
Related
I have a list say list1 and then I am making a copy of list1 as list2
Now if I remove one item from the list1 then the same item from list2 is also getting deleted.
ArrayList<Object> list1=new ArrayList<>();
//Then I am filling up only list1.
ArrayList<Object> list2=new ArrayList<>();
list1=list2;
for(i=0;i<Constants.list1.size();i++)
Constants.list1.remove(2);
Then if I write this:
if(Constants.list2.size()==0)
Toast.makeText(context,"Error",Toast.LENGTH_SHORT).show();
I am getting the Toast as ERROR.
I want to know whether copying one list to another makes them point to the same reference or not?
This code is not "copying":
list1 = list2;
It is making both variables (list1 and list2) point to the same List object. So changes are made to the same list object, regardless of which variable is used to reference it.
To make a copy of the list, you can use:
list1= new ArrayList<>(list2);
Or copy element by element:
for(i=0;i < list2.size();i++)
list1.add(list2.get(i));
This happens because you have two references (list1, list2)pointing to same memory object. It is a result of list1=list2 assignment.
If you want to make a clone of the initial list you need to use:
ArrayList<Object> list2 = new ArrayList<Object>(list1);
To avoid duplicate values first you need to clear the arraylist list2.clear(); then copy full arraylist
ArrayList<Object> list1=new ArrayList<>();
//Then I am filling up only list1.
ArrayList<Object> list2=new ArrayList<>();
list2.clear(); //Before add value clear the arraylist
list2.addAll(list1);
Another way to create a standalone copy of your list would be with using the clone function and casting it to the expected type:
Replace:
list1 = list2;
With this:
list1 = (ArrayList<Object>)list2.clone();
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
I want to combine two List Array into a Array single one.
first List looks like this:
List<String> first = new ArrayList<String>();
{a,s,d,f,g,h}
second list looks like this:
List<String> second = new ArrayList<String>();
{z,x,c,v,b}
I did the following:
ArrayList<String> combine = new ArrayList<String>();
combine.addall(first);
combine.addall(second);
{a,s,d,f,g,h,z,x,c,v,b}
But I want to combine both to be as
{{a,s,d,f,g,h},{z,x,c,v,b}}
How to do this in Java?
You should do something like this
ArrayList<String> l1=new ArrayList<String>();
l1.add("a");
ArrayList<String> l2=new ArrayList<String>();
l1.add("z");
List<ArrayList<String>> l3 = new ArrayList<ArrayList<String>>();
l3.add(l1);
l3.add(l2);
l1 and l2 are lists of strings, while l3 is a list of listOfStrings.
But are you sure you really want to do that? What about multidimensional arrays?
I know how to create an ArrayList of ArrayList, but how to add new ArrayList and add value to that particular ArrayList and how to retrieve the data from that list.
ArrayList<ArrayList<Integer>> arrayList = new ArrayList<ArrayList<Integer>>();
how to add new ArrayLists and ADD value to that particular ArrayList
and how to RETRIEVE the data form that list.
ArrayList<ArrayList<Integer>> arrayList=new ArrayList<ArrayList<Integer>>();
ArrayList<Integer> newAL= new ArrayList<Integer>();
arrayList.add(newAL);
newAL.add(1);
newAL.add(2);
newAL.clear();
System.out.println(arrayList.get(0));
//Changes persist in your arraylist
So after adding ArrayList you can manipulate newAL as ArrayList stores reference you don't need to fetch and set element from main arrayList.
To retrive data you can Iterate(Use ForEach Loop) over arrayList or you can do following
Integer List0Item1=arrayList.get(0).get(1);//Get first element of list at 0
arrayList.get(0).set(0, 10);//set 0th element of list at 0 as 10
ArrayList<Integer> list10=arrayList.get(10);//get arraylist at position 10
You can try this
ArrayList<ArrayList<Integer>> arrayList = new ArrayList<>();
ArrayList<Integer> list1=new ArrayList<>();
ArrayList<Integer> list2=new ArrayList<>();
list1.add(1); // add to list
list1.add(2);
list2.add(3);
list2.add(4);
arrayList.add(list1); // add to list of list
arrayList.add(list2);
for(ArrayList<Integer> i:arrayList){ // iterate -list by list
for(Integer integer:i){ //iterate element by element in a list
}
}
You can get element directly
arrayList.get(0).get(0); // 0th list 0th value
adding value to 0th list
arrayList.get(0).add(1); // 1 will add to 0th index list-list1
It is a list of list where every object of the parent list is in turn a sublist.The code is something like below:
List<List<String>> mainList = new ArrayList<List<String>>();
List<String> sublist1 = new ArrayList<String>();
sublist1.add("State");
sublist1.add("Country");
sublist1.add("City");
mainList .add(sublist1);
List<String> sublist2 = new ArrayList<String>();
sublist2.add("Sleep");
sublist2.add("Suspend");
sublist2.add("Wait");
mainList .add(sublist2);
for(List<String> obj:mainList){ // iterate
for(String value:obj){
System.out.println(value);
}
}
}
}
Hope this will help to clear your doubt.
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