Create morethan arraylist in dynamically - java

ArrayList<Employee> empList = new ArrayList<Employee>();
We want to create arraylist in dynamically for example :
empList1, empList2, empList3, empList4.....up to empList30.
Please help me.

You could use an array of ArrayLists instead:
ArrayList<Object>[] lists = new ArrayList[30];
for (int i=0;i<30;i++)
lists[i] = new ArrayList<Object>();
To get a item out of a list use the following code:
lists[0].get(1);
To add a new item:
lists[0].add(new Object());

use array:
ArrayList[] empList = new ArrayList[30];
for(int a=0;a<emList.length;a++)
empList[a] = new ArrayList();
or use ArrayList of ArrayLists:
ArrayList<ArrayList<Object>> empList = new ArrayList<ArrayList<Object>>();
and usage:
GET:
empList.get(0).get(3);
SET:
empList.get(0).set(4,new Object());

Related

List of Lists to 2d Array

i need to convert this list: List<double[]> test = new ArrayList<>();
to an 2 dimensional Array with an structure like this :
double[][] test2 = {{3.17, 26.348}, {3.65, 24.198}, {3.28, 25.085}, {3.37, 22.461},
{2.57, 23.740}, {3.60, 24.786}, {3.50, 23.374}, {2.98, 23.725},
{2.54, 23.227}, {3.41, 26.920}..........};
i already tried things like this but it did not work :
List<String[]> list=new ArrayList<String[]>();
double[][] matrix=new double[x34.size()][];
matrix=list.toArray(matrix);
If you are using Java 11 or higher, each collection class has a toArray method which you can use. Example:
List<double[]> myList = List.of(new double[]{3.17, 26.348},
new double[]{3.65, 24.198},
new double[]{3.28, 25.085},
new double[]{3.37, 22.461});
double[][] result = myList.toArray(double[][]::new);

How to split a String in Java and store it in different list

I have stored these comma separated String in an ArrayList.
[<b>SELL 512<\/b> lots of <b>xyz18#112.00<\/b>, <b>BUY 513<\/b> lots of <b>abc#113.00<\/b>]
I want to remove all the HTML Tags and then further I want to store them in different array List.
Say for eg I want my output something like this.
List3: [SELL, BUY]
List4: [512, 513]
List5: [xyz, abc]
List6: [112, 113]
List<String> List1 = new ArrayList<>();
List<String> List2 = new ArrayList<>();
List<List<String>> Tuples = new ArrayList<List<String>>();
List<String> List3 = new ArrayList<>();
List<String> List4 = new ArrayList<>();
List<String> List5 = new ArrayList<>();
List<String> List6 = new ArrayList<>();
for (int i = 0; i < List1.size(); i++) {
String var = List1.get(i).trim();
for (String x : var.split("\\<.*?\\>|\\#|\\,*$|\\.", 7)) {
List2.add(x);
}
Tuples.add(List2);
}
System.out.println(Tuples);
for (int i = 0; i < Tuples.size(); i++) {
List3.add(Tuples.get(i).get(1).split("\\s")[0].replaceAll("SELL","0").replaceAll("BUY","0"));
List4.add(Tuples.get(i).get(1).split("\\s")[1]);
List5.add(Tuples.get(i).get(3));
List6.add(Tuples.get(i).get(4));
}
System.out.println(List3);
System.out.println(List4);
System.out.println(List5);
System.out.println(List6);
}
But this fetches me output something like this:
List3: [SELL, SELL]
List4: [512, 512]
List5: [xyz, xyz]
List6: [112, 112]
If you inspect the output of System.out.println(Tuples); you'll notice a problem:
Tuples.add(List2) does not make a copy of List2. You're storing multiple references to the same List2 in Tuples, and appending more data to the same List2. So you end up with a very long list (i.e., List2) containing all tuples concatenated, and that list replicated many times inside Tuples.
Instead of
Tuples.add(List2);
try this instead:
Tuples.add(new ArrayList<>(List2));
List2.clear();
This will make elements of Tuples reference different lists.
I think you are doing it in a way too complicated way, just do:
List<String> inputList = new ArrayList<String>();
inputList.add("<b>SELL 512</b> lots of <b>xyz18#112.00</b>");
inputList.add("<b>BUY 513</b> lots of <b>abc#113.00</b>");
System.out.println("inputList: "+inputList);
List<List<String>> Tuples = new ArrayList<List<String>>();
List<String> list3 = new ArrayList<String>();
List<String> list4 = new ArrayList<String>();
List<String> list5 = new ArrayList<String>();
List<String> list6 = new ArrayList<String>();
for (int i = 0; i < inputList.size(); i++) {
String var = inputList.get(i).trim();
String[] splitArr=var.split("</b>|<b>|\\d*#"); //remove the \\d* if you expect to have xyz18 in output instead of x
list3.add((splitArr[1].split("\\s"))[0]);
list4.add((splitArr[1].split("\\s"))[1]);
list5.add(splitArr[3]);
list6.add(splitArr[4]);
Tuples.add(Arrays.asList(var.replaceAll("</b>|<b>", "")));
}
System.out.println("Tuples: "+Tuples);
System.out.println("list3: "+list3);
System.out.println("list4: "+list4);
System.out.println("list5: "+list5);
System.out.println("list6: "+list6);
OUTPUT:
inputList: [<b>SELL 512</b> lots of <b>xyz18#112.00</b>, <b>BUY 513</b> lots of <b>abc#113.00</b>]
Tuples: [[SELL 512 lots of xyz18#112.00], [BUY 513 lots of abc#113.00]]
list3: [SELL, BUY]
list4: [512, 513]
list5: [xyz, abc]
list6: [112.00, 113.00]

How do I create an array list of array lists in java? [duplicate]

This question already has answers here:
2d ArrayList in Java adding data
(2 answers)
Closed 7 years ago.
I currently have an array of arrays and that would work fantastic, except I just realized that I don't know the length of all of the arrays that I need. So I have to switch to ArrayList. I'm used to having an array of arrays and I know how to iterate through them. How do you do the same thing with ArrayLists?
The following works... until I need to change line size through my iterations.
Person[][] nameList = new Person[yearsBack][lines];
for (int i=0; i<yearsBack; i++) {
for (int j=0; j<lines; j++) {
nameList[i][j] = new Person(name, gender, rank);
This should do it:
List<List<Object>> listOfLists = new ArrayList<>();
To loop through:
for(List<Object> list : listOfLists){
for(Object obj : list){
//do stuff
}
}
ArrayList < Objects > can hold the Arraylist Objects and thus you can have the array of arraylists.
import java.util.ArrayList;
public class Test {
public static void main(String[] args) {
ArrayList<Object> mainAl = new ArrayList<Object>();
ArrayList<Integer> al1 = new ArrayList<Integer>();
ArrayList<String> al2 = new ArrayList<String>();
ArrayList<Double> al3 = new ArrayList<Double>();
ArrayList<ArrayList<Object>> al4 = new ArrayList<ArrayList<Object>>();
al1.add(1);
al1.add(2);
al1.add(3);
al1.add(4);
al2.add("Hello");
al2.add("how");
al2.add("are");
al2.add("you");
al3.add(100.12);
al3.add(200.34);
al3.add(300.43);
al3.add(400.56);
mainAl.add(al1);
mainAl.add(al2);
mainAl.add(al3);
for (Object obj : mainAl) {
System.out.println(obj);
}
al4.add(mainAl);
for (Object obj : al4) {
System.out.println(obj);
}
}
}
I hope this example is helpful for you.

Nested List In One List

How to convert a nested list into a single list in java?
List list = new ArrayList();
List newList = new ArrayList();
List<Integer> list1 = new ArrayList<Integer>();
list1.add(2);
list1.add(4);
list.add(5);
list.addAll(list1);
list.add(6);
How can i add the elements of list to newList so that when i print newList
it prints
[5,2,4,6]
Unless there is something you're not telling us, it is simply a repeat of what you have already done:
newList.addAll(list);
As has been said in the comments though, you really should be using full generic typing for your list variables:
List<Integer> list = new ArrayList<>();
List<Integer> newList = new ArrayList<>();
List<Integer> list1 = new ArrayList<>();
You shouldn't be using raw types, and you can call newList.addAll(list); -
public static void main(String[] args) {
List<Integer> list = new ArrayList<>(); // <-- diamond operator, Java 7 and up.
List<Integer> newList = new ArrayList<>();
List<Integer> list1 = new ArrayList<>();
list1.add(2);
list1.add(4);
list.add(5);
list.addAll(list1);
list.add(6);
newList.addAll(list); // <-- here
System.out.println(newList);
}
Output is the requested
[5, 2, 4, 6]
You already used a possible way with
list.addAll(list1)
Another way would be walking through the list resp. using it's iterator
to fetch and put all items into the newList.
Have a look at the API for more detailed info: http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#addAll(java.util.Collection)
for(int i=0;i<list.size();i++){
if(list.get(i) instanceof Integer){
newlist.add(list.get(i));
}else{
newlist.addAll((ArrayList)list.get(i));
}
}
The list that has to hold another list should be defined properly, rest working will be same. Refer below:
import java.util.List;
import java.util.ArrayList;
public class demo2 {
public static void main(String[] args) {
List<List<Integer>> nestedList = new ArrayList<>();
List<Integer> list = new ArrayList<>();
list.add(1);
nestedList.add(list);
System.out.println(nestedList);
}
}

Java convert Set<Set<String>> to List<List<String>>

In Java I am having trouble converting from a Set<Set<String>> to a List<List<String>> and then populating this list with the contents of the Set<Set<String>>
Here is my code:
Set<Set<String>> treeComps = compExtractor.transform(forest); // fine
List<List<String>> components = new List<List<String>>(); // does not work
components.addAll(treeComps); // does not work
You can't instantiate an instance of the List interface, you need to use one of the implementations like ArrayList. Then you can iterate over the outer set in treeComps, create a new ArrayList for each inner set, call addAll on this ArrayList and then add the list to components.
List<List<String>> components = new ArrayList<List<String>>();
for( Set<String> s : treeComps )
{
List<String> inner = new ArrayList<String>();
inner.addAll( s );
components.add( inner );
}
I think only way is iterate over outer set. Get inner set and user new ArrayList<String>(innerSet)
Add above result list to outerlist.
Something like this...
Set<Set<String>> treeComps = compExtractor.transform(forest);
List<List<String>> lists = new ArrayList<List<String>>();
for (Set<String> singleSet : treeComps) {
List<String> singleList = new ArrayList<String>();
singleList.addAll(singleSet);
lists.add(singleList);
}
List<List<String>> components = new Vector<List<String>>();
List<String> n;
for( Set<String> s : trerComps ) {
n = new Vector<String>();
n.addAll( s );
components.add( n);
}

Categories