-1

I do not know the suitable constructor for an Object array. Is there any constructor for such?

Object[] newArr = new Object[this.count];
List result = new List (newArr);

This line always error to:

error: no suitable constructor found for List(Object[])
                                        List newArr = new List(result);
constructor List.List(String[]) is not applicable
      (argument mismatch; Object[] cannot be converted to String[])
    constructor List.List(Integer[]) is not applicable
      (argument mismatch; Object[] cannot be converted to Integer[])

What should I do? I need to copy the new Object array from a this.arr (object array)

I honestly am confused with this part.

jane
  • 1
  • `List` is an interface, you can't create an instance of that. Use eg. `ArrayList`, as in `List result = new ArrayList<> (newArr); `. – daniu Nov 13 '21 at 09:43
  • 1
    @daniu List can create new instances (`List.of` for example), and ArrayList has no constructor that takes an object array. Your comment is entirely misleading from start to finish. – rzwitserloot Nov 13 '21 at 09:51
  • "*I do not know the suitable constructor for an Object array*" you do, since you already used it at `new Object[this.count];`. Your problem seems to be that you are using `new List(..)` which is invalid as `List` is an interface and we can't instantiate interfaces. You are probably looking for question like: [Converting array to list in Java](https://stackoverflow.com/q/2607289) – Pshemo Nov 13 '21 at 09:51

1 Answers1

0

If you want a List that is 'backed' by the array, you can use Arrays.asList(yourArray). Being 'backed' means:

  • This list can neither grow nor shrink, so, add, clear, etc don't work. .set does, though.
  • Any change made to the array is visible via the list.
  • Any change made to the list (with .set) is visible in the array.
  • There is no data duplication, which is relevant only if this array has a few tens of thousands of entries or more than that.

If you want a copy (meaning, the list and array are entirely separate; changes to one aren't applied to the other), you have a few options:

  • new ArrayList<>(Arrays.asList(yourArray));
  • List.of(yourArray);

The first one is mutable (meaning, the resulting list object can be changed in any way you like, such as with .add or .clear). The second one is immutable: It can't be changed at all. This can be useful; for example, you can hand an immutable list out to any code you want without worrying about them changing your list out from underneath you. With the mutable variant you'd have to hand out clones which gets annoying to write, and pricey in memory if this list is large.

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72
  • What is .set? And how should it be implemented? – jane Nov 13 '21 at 10:00
  • `.set(idx, value)` is a method in the List interface. You don't implement it; it is implemented for you (all List implementations have it, by definition). – rzwitserloot Nov 13 '21 at 13:10