2

im getting uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. error when adding array to linkedlist

Here is my work

LinkedList main_list = new LinkedList();
int arr = new int[2]
arr[0] = 0;
arr[1] = 1;

main_list.add(arr);
  • just state what is going to be into the list: `LinkedList main_list = new LinkedList<>()` (*or* ignore the warning) – user85421 Mar 18 '19 at 05:51
  • Do you want to put an `int[]` into the List or all `int` from that array separately? – Thilo Mar 18 '19 at 06:03
  • @toy moy, please accept the [answer](https://stackoverflow.com/a/55215326/3048967). I hope that you find it useful. – ScrapCode Mar 18 '19 at 06:54

4 Answers4

2

The compiler warning messages convey that the operation you are trying to is unsafe!

This comes up in Java 5 and later if you're using collections without type specifiers. (See generics )

Here you are creating a LinkedList() without specifying its type. It means that the compiler can't check that you're using the collection in a type-safe way, using generics.

You should create it like below by specifying its type.

LinkedList<int[]>  myList = new LinkedList<>();
ScrapCode
  • 2,109
  • 5
  • 24
  • 44
1

You can just have a list of Interger arrays:

LinkedList <int[]> main_list = new LinkedList <>();
int[] arr = {0,1};
int[] arr2 = {2,3};

main_list.add(arr);
main_list.add(arr2);

with this structure all of your Integer arrays will keep their initial boundaries and the result will not be stored in a long flat list. You can access them independently for later use.

Allan
  • 12,117
  • 3
  • 27
  • 51
0

First convert Array into list and then add it into list. If your Arrays only contain Integers then you create Integer List and array of Integers. Like

Integer[] arr = new Integer[2];
arr[0] = 0;
arr[1] = 1;

List<Integer> main_list = new LinkedList<>();

main_list.add(Arrays.asList(arr));

It will work. Or if you want to save array at every index of lined list then you have to create list of arrays of Integers. Like

List<Integer[]> main_list = new LinkedList<>();
Integer[] arr = new Integer[2];
arr[0] = 0;
arr[1] = 1;
main_list.add(arr);
Khalid Shah
  • 3,132
  • 3
  • 20
  • 39
  • I cant do that as im going to add more array to the list and that will mix the elements with a different array elements –  Mar 18 '19 at 05:42
  • is there a way to add the array as it is? –  Mar 18 '19 at 05:43
  • @toymoy see my updated answer. when yo add more arrays it will not mix the elements. It will append all array elemets to the previos elements – Khalid Shah Mar 18 '19 at 05:48
  • an array of int should work too. (note: last declaration of `arr` above is missing the `[]`) – user85421 Mar 18 '19 at 06:13
  • @CarlosHeuberger yeah int[] works too . Thanks for feed back :) updated. – Khalid Shah Mar 18 '19 at 06:23
0

You can add it directly by using Arrays.asList(arr). You can directly convert Array into list by using asList() function. Use the following code:

LinkedList main_list = new LinkedList(Arrays.asList(arr));
Kavita Patil
  • 1,784
  • 1
  • 17
  • 30