Java-arratlist-> ToArray () method __java

Source: Internet
Author: User

Api:http://docs.oracle.com/javase/7/docs/api/java/util/arraylist.html

ArrayList provides a very convenient way to convert a list to an array toarray. The ToArray has two overloaded methods:
1.list.toarray ();
2.list.toarray (t[] a); For the first overloaded method, the list is converted directly to a object[] array, and the second is to convert the list to an array of the types you want, and of course we will convert it to the same type as the list content.

Generally speaking, we all write this:
    arraylist<string> list=new arraylist<string> ();
        for (int i = 0; i < i++) {
            list.add ("" +i);
        }

        String[] array= (string[]) List.toarray (); Arraylist<string> list=new arraylist<string> ();
        for (int i = 0; i < i++) {
            list.add ("" +i);
        }

        String[] array= (string[]) List.toarray ();
However, the run error, why is this? 
The error prompts are as follows: Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; Cannot is cast to [Ljava.lang.String ;

Object[] can not be converted into string[];   The need for individual conversion of each element

so correct should be:
    object[] arr = List.toarray ();
    for (int i = 0; i < arr.length i++) {
         String e = (string) arr[i];
         System.out.println (e);
    }

As can be seen from the example, the first overload method ToArray () is not perfect, and the second overload method will appear efficient, practical ~

Below I post two kinds of construction method realization process, everybody can realize by oneself

public object[] ToArray ();
        {object[] result = new Object[size];
        System.arraycopy (elementdata, 0, result, 0, size);
    return result; Public object[] ToArray (Object a[]); {if (A.length < size) A = (object[]) java.lang.reflect.Array.newInstance (A.getclass (). Getcomponentt
        Ype (), size);
        System.arraycopy (elementdata, 0, a, 0, size);  if (a.length > Size) a[size] = null;
    @1 return A; }

@1 Place, why is it null?
: If you're having a similar problem, the first thing you do is look at the API and stare, not just contemplate
it. Api:[http://docs.oracle.com/javase/7/docs/api/java/util/arraylist.html#toarray (T[])] (http://docs.oracle.com/ Javase/7/docs/api/java/util/arraylist.html#toarray%28t%5b%5d%29)
     * <p>if the list fits in the specified Array with room to spare
     * (i.e., the "array has more elements than the" list), the element in
     * The array immediate Ly following the end of the "collection is" set to
     * <TT>NULL</TT>.  (This is useful in determining the length of the
     * list <i>only</i> If the caller knows the list does Not contain
     * any null elements.)
*/
Only when the caller knows that the list does not contain any null elements can this method determine the length of the list ",
there is no null element in the original list, so it a[size] = null; When
assigned, it is useful to determine the list length .
If there is a null element in the previous list, there is no way to tell which is the true length.
Use Demo:
1). parameter specifies an empty array, saving space
string[] y = X.toarray (new string[0]);
2. Specify large array parameters to waste time, using the reflection mechanism
string[] y = X.toarray (new string[100]);  Assume that the array size
is greater than 3. Let's say the best.

Welcome to ask your question ~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.