2

I have this:

data.put(1, 
            new Object[] { listUserColumns.get(field++).getName(), listUserColumns.get(field++).getName(),
            listUserColumns.get(field++).getName(), listUserColumns.get(field++).getName(),
            listUserColumns.get(field++).getName(), listUserColumns.get(field++).getName(),
            listUserColumns.get(field++).getName(), listUserColumns.get(field++).getName(),
            listUserColumns.get(field++).getName(), listUserColumns.get(field++).getName(),
            listUserColumns.get(field++).getName(), listUserColumns.get(field++).getName(),
            listUserColumns.get(field++).getName(), listUserColumns.get(field++).getName(),
            listUserColumns.get(field++).getName(), listUserColumns.get(field++).getName(),
            listUserColumns.get(field++).getName(), listUserColumns.get(field++).getName(),
            listUserColumns.get(field++).getName(), listUserColumns.get(field++).getName(),
            listUserColumns.get(field++).getName(), listUserColumns.get(field++).getName(),
            listUserColumns.get(field++).getName(), listUserColumns.get(field++).getName() });

And i want to cast this listUserColums (List of type String) directly to new Object[]( of type java.lang.object) but don`t know how?

GBlodgett
  • 12,704
  • 4
  • 31
  • 45
  • What does each call to `getName()` return? –  Aug 07 '18 at 13:32
  • Possible duplicate of [Convert List to String\[\] in Java](https://stackoverflow.com/questions/14773264/convert-listobject-to-string-in-java) –  Aug 07 '18 at 13:32
  • 1
    `Object[] objects = listUserColumns.stream().map(o -> o.getName()).toArray()` – ernest_k Aug 07 '18 at 13:34
  • What you have there is not really a `List` though, is it? It looks like a list of some sort of class that has a `getName()` method. – RealSkeptic Aug 07 '18 at 13:35
  • 1
    Your code does not seem consistent with your description. If `listUserColumns` has type `List` then the elements you retrieve from it do not have a method named "getName". If it *were* really a `List`, though, then `List.toArray()` would do what you want. – John Bollinger Aug 07 '18 at 13:35

3 Answers3

1

It depends on what you are intended to obtain and it is not clear from the question.

When you need to get array of names for all elements in your list, you can use Stream<T>

Object[] names = listUserColumns.stream().map(c -> c.getName()).toArray();

However, there is no way to cast this listUserColums, since array is the inner representation of ArrayList. You can create a new array that contains references to the same elements as the list with this

Object[] elements = listUserColumns.toArray();

In your case it looks like listUserColumns is not List<String>, that's why the results of these methods would be different (In the first case array of string, and in the second case array of your custom types).

Nikolay K
  • 3,770
  • 3
  • 25
  • 37
1

It appears that what you want is more like

data.put(1, listUserColumns.stream().map(c -> c.getName()).toArray());

for a sub set

data.put(1, listUserColumns.subList(from, to)
                           .stream().map(c -> c.getName()).toArray());
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
0

You can do the toArray() method:

ArrayList<String> arr = new ArrayList<>();
arr.toArray()

Which:

Returns an array containing all of the elements in this list in proper sequence (from first to last element).

GBlodgett
  • 12,704
  • 4
  • 31
  • 45