Questions tagged [toarray]

toarray, to convert from arraylist to array this method can be used. Being language specific for e.g. in Java its its available with ArrayList and in C# its used with List. Use this tag for toarray related questions.

toArray() Returns an array containing all of the elements in this list in the correct order.

Resource

136 questions
110
votes
4 answers

java: (String[])List.toArray() gives ClassCastException

The following code (run in android) always gives me a ClassCastException in the 3rd line: final String[] v1 = i18nCategory.translation.get(id); final ArrayList v2 = new ArrayList(Arrays.asList(v1)); String[] v3 =…
Gavriel
  • 18,088
  • 12
  • 63
  • 98
35
votes
8 answers

Java List T[] toArray(T[] a) implementation

I was just looking at the method defined in the List interface: T[] toArray(T[] a) , and I have a question. Why is it generic? Because of that fact, method is not complete type-safe. The following code fragment compiles but causes…
midas
  • 1,488
  • 2
  • 16
  • 20
24
votes
5 answers

Lock vs. ToArray for thread safe foreach access of List collection

I've got a List collection and I want to iterate over it in a multi threaded app. I need to protect it every time I iterate it since it could be changed and I don't want "collection was modified" exceptions when I do a foreach. What is the correct…
Bryan Legend
  • 6,314
  • 1
  • 54
  • 56
23
votes
2 answers

Can I rely on the LINQ ToArray() always returning a new instance?

I'm looking for an easy way to clone off an IEnumerable parameter for later reference. LINQ's ToArray extension method seems like a nice, concise way to do this. However, I'm not clear on whether it's always guaranteed to return a new array…
Joe White
  • 87,312
  • 52
  • 206
  • 320
20
votes
3 answers

Why does Java's Collection.toArray() return an Object[] rather than an E[]?

Before Java generics, Collection.toArray() had no way to know which type of array the developer expected (particularly for an empty collection). As I understand it, this was the main rationale behind the idiom collection.toArray(new E[0]). With…
12
votes
5 answers

Java List toArray(T[] a) implementation

I was just looking at the method defined in the List interface: Returns an array containing all of the elements in this list in the correct order; the runtime type of the returned array is that of the specified array. If the list fits in the…
Oscar Gomez
  • 18,102
  • 12
  • 80
  • 113
11
votes
1 answer

No suitable method found for ArrayList .toArray(String[]::new) in return statement

I am working on the site Codingbat, specifically this method in AP-1 public String[] wordsWithout(String[] words, String target) { ArrayList al = new ArrayList<>(Arrays.asList(words)); al.removeIf(s -> s.equals(target)); return…
Alex Pickering
  • 121
  • 1
  • 6
11
votes
2 answers

Difference between toArray(T[] a) and toArray()

I've been learning how to program with java and I haven't got any clear explanation about the difference of LinkedList's toArray(T[] a) and toArray() method. The second one simply returns all of the elements within the LinkedList object as an array,…
Logos
  • 302
  • 1
  • 6
  • 14
10
votes
1 answer

Does adding .ToArray or .ToList always make database queries faster?

I've noticed that database queries run faster when adding .ToArray() or .ToList() to queries. Is this because the data set is loaded into memory and all subsequent queries are done in-memory rather than doing further expensive database calls? What…
MariS
  • 103
  • 1
  • 6
9
votes
3 answers

List class's toArray in Java- Why can't I convert a list of "Integer" to an "Integer" array?

I defined List stack = new ArrayList(); When I'm trying to convert it to an array in the following way: Integer[] array= stack.toArray(); I get this exception: Exception in thread "main" java.lang.Error: Unresolved compilation…
Numerator
  • 1,299
  • 3
  • 21
  • 37
8
votes
3 answers

Converting a list to an array with ToArray()

I've created a class called listItem and the following list: List myList = new List(); At some point in my code, I want to convert it to an array, thereby using: listItem[] myArray = myList.ToArray(); Unfortunately, this…
Jodll
  • 235
  • 2
  • 3
  • 6
8
votes
5 answers

In Java (1.5 or later), what is the best performing way to fetch an (any) element from a Set?

In the code below, I needed to fetch an element, any element, from toSearch. I was unable to find a useful method on the Set interface definition to return just a single (random, but not required to be random) member of the set. So, I used the…
chaotic3quilibrium
  • 5,066
  • 5
  • 48
  • 73
8
votes
2 answers

Java: how to implement `toArray` for `Collection`

Right now, I have: public T[] toArray(T[] old) { T[] arr = Arrays.copyOf(old, old.length + size()); int i = old.length; for(E obj : this) { arr[i] = old.getClass().getComponentType().cast(obj); …
Albert
  • 57,395
  • 54
  • 209
  • 347
7
votes
2 answers

List.toArray(Object[]) performance

I'm getting a List of object A, then I use Apache Commons Collection4 to transform the obtained List from having A instances to having B instances. listOfBs = (List) CollectionUtils.collect(listOfAs, componentTransformer); However,…
Muhammad Gelbana
  • 3,640
  • 3
  • 34
  • 76
7
votes
2 answers

In c# does Array.ToArray() perform a DEEP copy?

This should be a pretty basic question, but I've been having a little trouble finding a definite answer. When you have an array of values and you use the .ToArray() method does it create a deep or shallow copy of the array?
hrh
  • 630
  • 1
  • 13
  • 23
1
2 3
9 10