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?
Related
This question already has answers here:
Add ArrayList to another ArrayList in java
(6 answers)
Closed 6 years ago.
I have a 2D arraylist which I want to fill it with several 1D arraylists. My code is the following:
ArrayList<ArrayList<String>> array = new ArrayList<ArrayList<String>>();
ArrayList<String> list = new ArrayList<String>();
while (ts2.next()) {
list.add( ts2.getString("userName");
list.add(ts2.getString("userId"));
array.add(list);
list.clear();
}
I have noticed that list.clear() deletes the elements from the list however also deletes the element from the array. In the end, both array and list are empty. How can I add list in array and clear the list after array.add(list)
You can clone the list:
array.add(list.clone());
Or you can instantiate the list object within the loop itself:
ArrayList<ArrayList<String>> array = new ArrayList<ArrayList<String>>();
while (ts2.next()) {
ArrayList<String> list = new ArrayList<String>();
list.add( ts2.getString("userName");
list.add(ts2.getString("userId"));
array.add(list);
}
Then you wouldn't even need to clear it.
You can do this:
ArrayList<String[]> arr = new ArrayList<String[]>();
String[] str = new String[2];
while (ts2.next()) {
str[0] = ts2.getString("userName");
str[1] = ts2.getString("userId");
arr.add(str);
}
if you would an arraylist also:
ArrayList<ArrayList<String>> arr = new ArrayList<ArrayList<String>>();
while (ts2.next()) {
ArrayList<String> arrString = new ArrayList<String>();
arrString.add(ts2.getString("userName"));
arrString.add(ts2.getString("userId"));
arr.add(arrString);
}
You can create a new instance of ArrayList<> to be added in the existing one.
while (ts2.next()) {
List<String> list = new ArrayList<>();
list.add(ts2.getString("userName"));
list.add(ts2.getString("userId"));
array.add(list);
}
However the better approach is to map these values into a new instance of a class.
List<MyClass> = new ArrayList<>();
while (ts2.next()) {
MyClass i = new MyClass(ts2.getString("userName"), ts2.getString("userId"));
array.add(i);
}
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]
I am using Eclipse Juno and Java.
I want to create a list and then store that list in another list so I can pass the list of lists to the server side. I have tried:
ArrayList<T> listAccountAndCubs = new ArrayList<Comparable>();
listAccountAndCubs.add(accountId);
listAccountAndCubs.add(sqlDateArchived);
However, I can not get the values "T" and "Comparable" correct. I tried "String" however that does not work for storing the date.
Once the above is correct how do I set up the list to contain "listAccountAndCubs"?
Thanks for any assistance,
Glyn
this is how you can create a list
List<String> l = new ArrayList<String>();
this is how you can create list of list
List<List<Comparable>> listOfList = new ArrayList<List<Comparable>>();
listOfList.add(new ArrayList<Comparable>());
...
Sounds like you want something like this
List<List<String>> listAccountAndCubs = new ArrayList<List<String>>();
I would recomment using Google Guava library to clean the syntax a bit
List<List<String>> listAccountAndCubs = Lists.newArrayList();
List<ArrayList<Comparable>> listAccountAndCubs = new ArrayList<>();
or
List<String> l1=new ArrayList<>();
List<List<String>> l2=new ArrayList<>();
l1.add("a");
l2.add(l1);
If I understand you crrectly you want to have a list of Strings, and store this in another list?
List<String> sl = new ArrayList<String>();
List<List<String>>sls = new ArrayList<List<String>>();
sls.add(sl);
sl.add("String 1");
The value "T" is just a placeholder for the type, as the list is a generic interface, which can take any arbitrary object.
If you want to create a list of unspecified types, you would use
List<?>list = new ArrayList<?>();
Then you can add untyped objects to it, but in your case this is not neccessary.
Instead you can of course also create a list of comparables. Like this:
List<Comparable<String>>list = new ArrayList<Comparable<String>>();
i want to store Arraylist inside Another List, some thing like this i know its wrong but pretty much like this....
List list=new ArrayList();
list.add(new("element1","element2","element3",element4=?);
list.add(new("element5","element6","element7",element8=?);
now if u see the above code its ok and fine to add elements or first row to list up to third element but the fourth element is another array[string array] how to add it or append it to the first row of element.
same with the second row.
NOTE:- element4 and element8 are also differ in length means element4 has 2 string and element 8 has 10 strings.
when i display the list it should show list like this
The first row of list is
element1 element2 element3 element4.1 element4.2
the second row of list is
element5 element6 element7 element8.1 element8.2 element8.3 element8.4 element8.5 element8.6 element8.7 element8.9 element8.10
So from what I understood about your question, you need a List<List<String>>. Do it as follows:
List<String> strs1 = new ArrayList<String>();
strs1.add("element1");
strs1.add("element2");
List<String> strs2 = new ArrayList<String>();
strs2.add("element3");
And then
List<List<String>> listOfList = new ArrayList<List<String>>();
listOfList.add(strs1);
listOfList.add(strs2);
OR
List<List<String>> asList = Arrays.asList(strs1, strs2);
Have you tried following way:
List list = new ArrayList();
list.add(Arrays.asList("element1", "element2", "element3",
Arrays.asList("element4.1", "element4.2")));
list.add(Arrays.asList("element5", "element6", "element7",
Arrays.asList("element8.1", "element8.2", "element8.3")));
// print values
System.out.println(list.get(0));
System.out.println(list.get(1));
Unify it to a List of lists of lists, a three dimensional matrix - even if your first elements are single values, it makes sense to wrap them in lists just to simplify the code:
List<List<List<String>>> matrix = new ArrayList<List<List<String>>>();
List<List<String>> row = new ArrayList<List<String>>();
matrix.add(row);
List<String> column1 = new ArrayList<String>();
column1.add("element1");
row.add(column1);
List<String> column2 = new ArrayList<String>();
column1.add("element2.1");
column1.add("element2.2");
row.add(column2);
You can add lists to other lists by doing something like so: List<List<String>> myList = new ArrayList<List<String>>();.... However, seeing that you are adding items which have a different type, I would recommend you do the following (assuming you always have 3 array lists and 1 array):
Create a new class which takes in 4 arguments, these being the 3 array lists and the 1 array.
Have your class override its own toString() method in such a way that it will iterate over the elements and print their content in whatever way you would like.
Create an list using generics, using something like this: List<MyClass> myList = new ArrayList<MyClass>();.... In this case, MyClass is the class I have described in point 1. This will allow you to create a type safe structure which does not need to do any casting, thus making your code look cleaner and probably run slightly faster.
Seeing that you say that the elements can contain list of strings of various lengths, you can do something like this:
public class MyClass
{
private List<String> arrayList1;...
private String[] myArray;...
public MyClass(List<String> list1, ..., String[] myArray)
{
this.arrayList1 = list1;
this.myArray = myArray;
...
}
...
#Override
public String toString()
{
StringBuilder sb = new StringBuilder();
//iterate adding your list contents to your string builder.
return sb.toString();
}
}
Adding your elements then will be something like this:
...
List<String> arrayList1 = ...;
arrayList1.add("...");...
String[] myArray = ...;
MyClass myClass = new MyClass(arrayList1, ..., myArray);
System.out.println(myClass.toString());
ArrayList<String> veri1 = new ArrayList<String>();
String[] veri2 = {"Fatih", "Ferhat", "Furkan"};
How can I add veri2 to veri1 like one element? I mean, if I call veri.get(0), it returns veri2.
You should declare your list as a list of string arrays, not a list of strings:
List<String[]> veri1 = new ArrayList<String[]>();
String[] veri2 = {"Fatih", "Ferhat", "Furkan"};
veri1.add(veri2);
Note that in general it is better to declare your list as List instead of ArrayList, as this leaves you the freedom to switch to a different list implementation later.
You should use the List interface and generics (for Java >= 1.5). Depending on what you want to do you can use this:
String[] veri2 = {"Fatih", "Ferhat", "Furkan"};
List<String> veri1 = new ArrayList<String>();
veri1.addAll(Arrays.asList(veri2)); // Java 6
List<String[]> veri3 = new ArrayList<String[]>();
veri3.add(veri2);
You can't actually do this.
veri2 is an array of strings, veri1 is an arraylist of individual strings
Thus, doing veri1.get(0) should return a single string, not an array of strings.
I just saw (due to fm), that you have an ArrayList<String>. You can do:
ArrayList veri1 = new ArrayList();
veri1.add(veri2)
or
ArrayList<String[]> veri1 = new ArrayList<String[]>();
veri1.add(veri2)
You can also make ver1 a List, which gives you flexibility in changing implementations.
It all depends on whether or not you want your ArrayList to be of one type or if you need it to hold multiple types.
If you just need it to hold String arrays throughout your code, declare as stated above:
ArrayList<String[]> list1 = new ArrayList<String[]>();
then just add the String array to it as follows:
list1.add(stringArray);
If you want it to be dynamic, declare it with the object type:
ArrayList<Object> anythingGoes = new ArrayList<Object>();
and then you can add anything later on as well:
anythingGoes.add(stringArray);
anythingGoes.add(myAge);
anythingGoes.add(myName);
I think you mean this:
import java.util.Arrays;
ArrayList<String> veri1 = new ArrayList<String>();
String[] veri2 = {"Fatih", "Ferhat", "Furkan"};
veri1.addAll(Arrays.asList(veri2);
You pretty much just need to add the array to the ArrayList.
ArrayList<String[]> veri1 = new ArrayList<String[]>();
String[] veri2 = {"a", "b", "c"};
veri1.add(veri2);
System.out.println(veri1.size());
for(String[] sArray : veri1)
for(String s : sArray)
System.out.println(s);