List of Lists to 2d Array - java

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);

Related

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]

Remove duplicates in Long array

I have two Long array elements and both have some values
Long[] firstArray = new Long[10];
Long[] secondArray = new Long[25];
Both the array may or may not be equal in size.
firstArray[0] = new Long("1");
firstArray[1] = new Long("2");
firstArray[2] = new Long("3");
secondArray [0] = new Long("2");
secondArray [1] = new Long("3");
I want to compare the secondArray with firstArray and create a new thirdArray with the values which are not in secondArray.
In the above example the thirdArray will have only 1
One possible solution would be to convert both of your arrays to List and use removeAll:
Long[] firstArray = new Long[10];
Long[] secondArray = new Long[25];
firstArray[0] = new Long("1");
firstArray[1] = new Long("2");
firstArray[2] = new Long("3");
secondArray [0] = new Long("2");
secondArray [1] = new Long("3");
List<Long> first = new ArrayList<>(Arrays.asList(firstArray));
List<Long> second = Arrays.asList(secondArray);
first.removeAll(second);
Long[] thirdArray = first.toArray(new Long[first.size()]);
It would be more efficient to dump one of the arrays into the set, so you can perform fast search:
Set<Long> set = new HashSet<>(Arrays.asList(second));
After that you can use:
List<Long> list = new ArrayList<>(Arrays.asList(first));
list.removeAll(set);
return list.toArray(new Long[list.size()]);
Or simpler in Java-8:
return Stream.of(first).filter(e -> !set.contains(e)).toArray(Long[]::new);
If you need to keep track of the amounts of repeating elements, you could use a Map<Long, Long>.
Build the map (either a simple for or with stream() and groupingBy()), then iterate over the second array and decrease the count for each key, and then iterate over map's pairs and build the array again.
In Java 8:
Set<Long> set = Stream.of(secondArray).collect(Collectors.toSet());
Long[] thirdArray = Stream.of(firstArray).filter(e -> !set.contains(e)).toArray(Long[]::new);

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);
}
}

Create morethan arraylist in dynamically

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());

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