-3

i want to convert from List<List<Integer>> to List<int[]> but i don't know how to do it.

I try to do it :

List<Integer> list = new ArrayList<>();
List<List<Integer>> arr = matrix(list);
int [] temp2 = arr;

But it was wrong

PM 77-1
  • 12,933
  • 21
  • 68
  • 111
  • What is `matrix(...)`? Is that a method that you have written? Is it something that you have "pulled out of the air" ... in the hope that it might work? – Stephen C Apr 15 '17 at 03:42
  • `int[][] r = new int[arr.size()][]; IntStream.range(0, r.length).forEach(i -> r[i] = arr.get(i).stream().mapToInt(Integer::intValue).toArray());` – Elliott Frisch Apr 15 '17 at 03:43
  • 1
    Almost a duplicate of http://stackoverflow.com/questions/960431/how-to-convert-listinteger-to-int-in-java – Stephen C Apr 15 '17 at 03:45
  • The real issue is that a function called matrix should not be using a List to begin with. primitive computations should be done with un-boxed primitives, arrays, and IntStream/DoubleStream/LongStream etc. as there is a significant performance impact. – Novaterata Apr 15 '17 at 03:49
  • This was List to int[] but i ask List> to List ? – ken dragonz Apr 15 '17 at 03:54
  • @kendragonz I've provided you 3 ways to do it, including < Java 8 way. If this isn't good enough, you need explain what you are actually trying to do. You just have a "java" tag, if you can't use modern java, then you need to be more specific. – Novaterata Apr 15 '17 at 04:25

2 Answers2

1

I am not sure what you really want to achieve, but a simple solution would be a double loop to convert each List<Integer> to int[]. And you can do it with a simple method as AFAIK there is no built-in library doing this for you (because of the heterogeneity of types List and int[]):

// cycle this for each List<Integer>
int[] toIntArray(List<Integer> a_list){
    int[] ret = new int[a_list.size()];
    for(int i = 0; i < ret.length; i++)
      ret[i] = a_list.get(i);
    return ret;
}

Update Java 8

Thanks to Java 8 streams now you can simply write (again the example is for a single List<Integer>:

int [] my_ints = list.stream().mapToInt(Integer::intValue).toArray();

Update

As you asked for the complete solution, I will write down a possible method to achieve what you need:

List<int[]> intArrayList = new ArrayList();

for(List<Integer> intList : intListList){
    int[] intArray = new int[intList.size()];
    for(int i = 0; i < intArray.length; ++i){
       intArray[i] = intList.get(i);
    }
    intArrayList.add(intArray);
}
Alex Gidan
  • 2,619
  • 17
  • 29
0
 i want to convert from List<List<Integer>> to List<int[]> 

The return value from matrix() that you called arr for some reason is called intListList below.

// You can map the List<Integer> items in the List<List<Integer> to int[] items in a List<int[]> like so:

List<int[]> intArrList = intListList.stream()
    .map(Collection::stream)
    .map(Stream::mapToInt)
    .map(IntStream::toArray)
    .collect(Collectors.toList())

// or if you prefer

List<int[]> intArrList = intListList.stream()
    .map(ints -> ints.stream().mapToInt().toArray())
    .collect(Collectors.toList())

// For Java before 8
List<int[]> intArrList = new ArrayList();
for(List<Integer> ints : intListList){
    int[] intArr = new int[ints.size()];
    for(int i = 0; i < intArr.length; ++i){
       intArr[i] = ints.get(i);
    }
    intArrList.add(intArr);
}
Novaterata
  • 4,356
  • 3
  • 29
  • 51
  • thank you but i don't allow import new library , can you do it in another way ? – ken dragonz Apr 15 '17 at 04:00
  • @kendragonz This is not a new library, this is absolutely plain Java 8. If you can't use Java 8 then you need to add java-7 or android or some other tag. – Novaterata Apr 15 '17 at 04:01
  • @ajb the toArray() you are referring to creates an Object array, not an int[] – Novaterata Apr 15 '17 at 04:18
  • List intArrList = new ArrayList(); for(List ints : list){ int[] intArr = new int[ints.size()]; for(int i = 0; i < intArr.length; ++i){ intArr[i] = ints.get(i); } intArrList.add(intArr); } is this a function or something else ? – ken dragonz Apr 15 '17 at 06:46
  • It's a statement. An assignment statement. – Novaterata Apr 15 '17 at 11:37
  • how can i use it ? – ken dragonz Apr 15 '17 at 12:05
  • I'm not going to explain all aspects of Java to you in comments. This is what you asked for. PS this is not a website for doing your homework or reading your mind. – Novaterata Apr 15 '17 at 13:23
  • This literally takes a List of Lists of Integers and turns it into a List of arrays if ints. That is what you asked for. If not, ask another question. – Novaterata Apr 15 '17 at 13:26