The original Java has two Arraylist_java

Source: Internet
Author: User

First, give a piece of code:

public class Aslistmethod {public
 static void Main (string[] args) {
  String sentence = ' I love you ';
  list<string> words = arrays.aslist (Sentence.split (""));
  for (int i = 0; i < words.size ();) {
   if (words.get (i). Equals ("Love")) {
    words.remove (i);
    Continue;
   }
   i++;
  }
  SYSTEM.OUT.PRINTLN (words);
 }

Then, run this code, and finally, the error:

Exception in thread ' main ' java.lang.UnsupportedOperationException at
 java.util.AbstractList.remove ( abstractlist.java:161) at
 list. Aslistmethod.main (Aslistmethod.java:12)

The stack bottom list for the wrong chain. Aslistmethod.main (aslistmethod.java:12) says this line of code:

Words.remove (i);

This mistake let me very confused, because I wrote this code before probably looked, arrays.aslist method returns is a ArrayList, I can not remove it? Next, because of a small mistake, the result of the arrays.aslist to Java.util.ArrayList, incredibly assigned value is not successful, I immediately have a blind cat encounter dead Mouse feeling. Then, follow the Arrays.aslist method has been point down, found that this ArrayList is not ArrayList. Arrays.aslist returns the Java.util.Arrays.ArrayList, which is just an internal class of Arrays. Its code is not long, here it is posted directly:

 private static class Arraylist<e> extends Abstractlist<e> implements Randomaccess, java.io.Serializable {
  Private static final long serialversionuid = -2764017481108945198l;

  Private final e[] A;
  ArrayList (e[] array) {a = Objects.requirenonnull (array);
  @Override public int size () {return a.length;
  @Override public object[] ToArray () {return a.clone ();
   @Override @SuppressWarnings ("unchecked") public <T> t[] ToArray (t[] a) {int size = size ();
   if (A.length < size) return arrays.copyof (THIS.A, size, (class< extends t[]>) a.getclass ());
   System.arraycopy (THIS.A, 0, a, 0, size);
   if (a.length > Size) a[size] = null;
  return A;
  @Override public E get (int index) {return a[index];
   @Override public E Set (int index, E element) {E oldValue = A[index];
   A[index] = element;
  return oldValue; @Override public int indexOf (Object o) {if (o==null) {
    for (int i=0; i<a.length; i++) if (a[i]==null) return i;
   else {for (int i=0; i<a.length; i++) if (O.equals (a[i)) return i;
  } return-1;
  @Override public boolean contains (Object o) {return indexOf (o)!=-1;
  @Override public spliterator<e> Spliterator () {return Spliterators.spliterator (A, spliterator.ordered);

 }
 }

From the source, it inherits from Abstractlist, and without the Remove method, I find the Abstractlist Remove method:

 Public E-Remove (int index) {
  throw new unsupportedoperationexception ();
 }

This is the source of the error message. Finally, it is known that the Arrays.aslist returned ArrayList does not support remove, in fact, Java.util.Arrays.ArrayList does not support any form of element deletion.
To sum up, the solution to this problem is indeed a bit blind cat touch the smell of dead rats, but, as long as I carefully look at the newspaper error:

At Java.util.AbstractList.remove (abstractlist.java:161)

Calling Java.util.AbstractList's Remove method is different from what I expected, and it clearly tells me the direction.

I hope this article will help you learn Java programming.

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.