0

I am trying to populate an array of generic types but its always empty after the Array.fill i am using here. What can I be doing wrong?

  private <T> CartField<T>[] getPopulatedCart(T field) {
    CartField<T> cart = new CartField<>(field);
    CartField<T>[] cartFields = new CartField[0];
    Arrays.fill(cartFields, cart);
    return cartFields;
  }

This always returns an empty array, i can verify when debugging that the cart field makes it, even when inspecting the cart on the same line as Arrays.fill(cartFields, cart) it shows the value but on the return the array is empty.

Any help or guidance would be appreciated.

1
  • 1
    Don't mix arrays and generics. Use a List<CartField<T>> instead. Commented Nov 17, 2018 at 20:19

2 Answers 2

1

Arrays.fill will populate every element of an array. Your array is of size zero, and so there are no array elements to populate.

If you are expecting an array with a single element, you should initialize it to have size one instead.

Sign up to request clarification or add additional context in comments.

1 Comment

Excellent, easy enough. Thanks!.
0
CartField<T>[] cartFields = new CartField[1];  // size should be not empty

Comments

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.