0

What I would like to achieve is to have a nested list (or array) with elements as lists (or arrays) consisting a pair of integers. This is to store some data and I would like to access those integers efficiently.

So I created a nested list in Java like this:

List combined = new ArrayList();
int x[] = new int[2];
List segl = Arrays.asList(x)
combined.addAll(segl);

the nested list will later be appended with more list like segl. Now I want to return a element from combined. But with combined.get(0) the returned element data type is object instead of a list like segl, so I couldn't do anything with it (eg. return the single int element from segl. I have no idea how to change it to a list or array.

4
  • You are using raw types all over the place. List and ArrayList expect a type parameter to be given: List<int[]>. Oh, and I suggest not to mix arrays and Lists, unless you have a compelling reason to do so. Commented Mar 1, 2021 at 7:57
  • As the previous comments points out, you are getting Objects because you are using raw types. However, could you expand on what you are trying to achieve? Is combined supposed to be a 'List of Lists'? (e.g. List<List<Integer>?) Commented Mar 1, 2021 at 8:00
  • @ Wouter van der Linde I'm new to Java. Just searched on raw type and trying out with parameterization. What I would like to achieve is to have a nested list (or array) with elements as lists (or arrays) consisting of 2 integers. This is to store some data and I would like to access those integers efficiently. Commented Mar 1, 2021 at 8:04
  • At this point you should not get worried about efficiency. If you need a pair of integers for some reason, you should create a class that collects these integers. Maybe a Point for x-y information, or a Location for latitude and longitude. The make a List<Point>. Commented Mar 1, 2021 at 9:40

1 Answer 1

1

Based on the information you provided in the comments, you want to have 'List of Lists' where the second List has two elements. Unfortunately you can't use arrays as the generic type of a List. So what I think that you want to do is probably this:

// This is a List which holds other Lists.
List<List<Integer>> combined = new ArrayList<>();
List<Integer seql = new ArrayList<>(2); // Initialize with a size of 2.
// add justs adds seql to combined. addAll will add all elements of seql to combines
combined.add(segl);

This makes it so that combined.get(0) returns a List<Integer>.

If you are using an array you have two options:

   // Using Arrays.asList(..) 
   List<List<Integer>> combined = new ArrayList<>();
   Integer[] seql = new Integer[2];
   combined.add(Arrays.asList(segl));

   //If Integer[] is not possible.
   List<List<Integer>> combined = new ArrayList<>();
   int[] seql = new int[2];
   List<Integer> seqlAsList = Arrays.stream(seql).boxed().collect(Collectors.toList());
   combined.add(seqlAsList );
Sign up to request clarification or add additional context in comments.

8 Comments

Using arrays: If you know how many lists (or arrays) you want to add to combined you could also try something like this. int[][] combined = new int[100][2]; Where you can then access each seql using indices: First element: combined[0][0] Second element: combined[0][1]
The no. of pairs to be added is unknow. I tired your method it indeed returned List<Integer> for the pair. Let's say I used 'List<Integer> aa = combined.get(0);'. But when I use 'int aaa = aa.get(0);' to get back the single integer it doesn't work. could you point out why?
Could you be more precise as to what 'doesn't work'? aa.get(0) should return an int. Did you add an int to aa before accessing it? (e.g. did you do something like aa.add(5)?
I think I understand, you are trying to access a non-existing element. (There is nothing at aa.get(0) so that call returns null) Because of Generics you can only add the Integer wrapper type to the list. If you do things like int aaa = aa.get(0) Java will automatically autobox that to an int (primitive) That doesn't work if aa.get(0) is nothing as Java tries to autobox a null-value. You will need to add elements to aa before accessing them. :)
I think I know where's the issue but I'm not sure how to fix it. Actually the integer pair seql is from somewhere else, so I now have int seg[] = new int[2]; seg[0]=5; seg[1]=4; List ss = Arrays.asList(seg); List<Integer> segl = ss; that's how segl is currently defined. Also as you can see aa is not empty. The error message is 'class [I cannot be cast to class java.lang.Integer ([I and java.lang.Integer are in module java.base of loader 'bootstrap')'.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.