-1

I tried converting it to array list and back using this

https://www.geeksforgeeks.org/conversion-of-array-to-arraylist-in-java/

but I'm getting a lot of error, I also tried this

How do I remove objects from an array in Java?

but it only works on strings for some reason, I did modify to make it for int but it's not working

int[] array = {1,2,3,4};
List<Integer> list = new ArrayList<Integer>(Arrays.asList(array));
list.removeAll(Arrays.asList(3));
array = list.toArray(array);

What I'm trying to do is this

        // copy 2 elements in array1 to array2
        // remove those 2 elements from array1 since I already used them
Ruijie Lu
  • 17
  • 5
  • What errors do you get? – JCWasmx86 Jul 18 '20 at 18:44
  • no suitable constructor found for ArrayList(List) constructor ArrayList.ArrayList(int) is not applicable (argument mismatch; no instance(s) of type variable(s) T exist so that List conforms to int) constructor ArrayList.ArrayList(Collection extends Integer>) is not applicable (argument mismatch; inference variable T has incompatible bounds upper bounds: Integer,Object lower bounds: int[]) where T is a type-variable: T extends Object declared in method asList(T...) No idea what this is talking about – Ruijie Lu Jul 18 '20 at 19:04
  • 1
    Does this answer your question? [How to convert int\[\] into List in Java?](https://stackoverflow.com/questions/1073919/how-to-convert-int-into-listinteger-in-java) – David Fisher Jul 18 '20 at 21:33

6 Answers6

1

Conversions to and from lists, etc., are very roundabout ways. All you need to do is to make a new array, omitting the entries you don't want.

In the simple case where you want to remove a single unique value:

int[] array = { 1,2,3,4 };
int[] newArray = new int[array.length - 1];
for (int i=0, j=0; i<array.length; i++) {
    if (array[i] != 3)
        newArray[j++] = array[i];
}

Regard this as an example from which you can construct your exact needs.

user13784117
  • 1,124
  • 4
  • 4
0

Can use you the functtion get using index?

   ArrayList.get(index) 

and then remove it from the list

   ArrayList.remove(index)
  • How do I do that? I'm a beginner, also my array isn't an array list yet, I'm having trouble converting it to an array list – Ruijie Lu Jul 18 '20 at 19:03
  • I think this code can help you: Integer [] array = {1, 2, 3,4}; ArrayList list = new ArrayList(Arrays.asList(array)); int numberOne = list.get(0); // return 1 list.remove(0); // remove it System.out.println(numberOne); – vitor carvalho Jul 18 '20 at 19:14
0
Integer[] array = { 1,2,3,4 };
List<Integer> integerList = new ArrayList<>(Arrays.asList(array));
integerList.removeAll(List.of(1,2));
        
array = integerList.toArray(new Integer[integerList.size()]);

You have to replace old array with new (different length).

adian
  • 278
  • 1
  • 9
0

Here is a way to do it if you need to get back to an array of primitives (int instead of Integer).

int[] array = {1,2,3,4};
int[] array2 = {3};

Set<Integer> numbersToRemove = IntStream.of(array2).boxed().collect(Collectors.toSet());

array = IntStream.of(array).boxed().filter(e -> !numbersToRemove.contains(e)).mapToInt(e -> e).toArray();
jnorman
  • 606
  • 8
  • 14
0

Java generics does not work for primitives; as such, your int array is treated as one object for Arrays.asList, so it returns List<int[]>. You can use streams and filter for this purpose.

final List<Integer> list = Arrays.stream(array).boxed().filter(i->i!=3).collect(Collectors.toList());
System.out.println(list);

Demo!

If you need to remove more than one number, you can store them in a Set.

final Set<Integer> shouldRemove = Set.of(3, 6, 7, 10);
final List<Integer> list = Arrays.stream(array).boxed().filter(i->!shouldRemove.contains(i)).collect(Collectors.toList());
System.out.println(list);
Unmitigated
  • 76,500
  • 11
  • 62
  • 80
0

This is because it's calling public static <T> List<T> asList(T... a) which can take multiple arguments but you're only passing the 1 of the type int[] and thus type T is of that type.

You can verify this:

System.out.println(Arrays.asList(array).get(0)[0]); // returns 1
System.out.println(Arrays.asList(array).get(0)[1]); // returns 2
System.out.println(Arrays.asList(array).get(0)[2]); // returns 3
System.out.println(Arrays.asList(array).get(0)[3]); // returns 4

Java is pretty dumb when it comes to primitives and what you want to do with them. Primitives cannot be a type in Java so it's doing the best it can in an effort to get you what you're asking of it.

Change your int[] to an Integer[] and it should work.

Integer[] array = {1, 2, 3, 4};
David Fisher
  • 282
  • 2
  • 13