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
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 am getting an object array inside a arraylist that is also of Object type. I know within the very first index of the object array list I have a nested object array and I have to retrieve that array. How can I do this? Right now I am iterating over complete object array list which is not good as I know I have to retrieve the object array from the very first object list element.
List<String> resultList = new ArrayList<String>();
List<Object[]> listObj = (List<Object[]>)query.getResultList();
for(Object[] obj: listObj){
resultList.add(((String)obj[0]));
resultList.add(((String)obj[1]));
}
You should take a look at this answer because this is exactly what you want:
https://stackoverflow.com/a/8882358/9016740
You can access the first element of an ArrayList by using the get(index) method as such:
List<String> resultList = new ArrayList<String>();
List<Object[]> listObj = (List<Object[]>)query.getResultList();
Object[] firstObjArray = listObj.get(0);
resultList.add(((String)firstObjArray[0]));
resultList.add(((String)firstObjArray[1]));
Try this.
Object[] resultArr = listObj.get(0);
Now use resultArr to fetch values and store in resultList.
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 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 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));