CodeGym-17~20

读文章

0.如果是基本数据类型的话,在数组中就存储特定的值;如果是对象的话,在数组中就是存储对象的引用。

1.数组本身就是对象

再读文章

0.Arrays.sort(array);  Arrays.toString(array);(重写了toString方法)  Arrays.copyOf(array,length);(返回复制以后的数组,也可以复制二维数组)  Arrays.copyOfRange(array, start_index,end_index);

1.数组类并没有重写equals方法,但是Arrays类重写了equals方法。Arrays.equals(array1,array2);  Arrays.deepEquals(array1,array2);  Arrays.deepToString(array1,array2);

又读文章

0.

    ArrayList的size不定的根据:

    • When the internal array is filled, ArrayList creates a new array internally. The size of the new array is the size of the old array times 1.5 plus 1.
    • All the data is copied from the old array into the new one。
    • The old array is cleaned up by the garbage collector.

  indexOf();  get();  add();  contain();  set();(与add有区别)  clear();  asList();{ArrayList<Cat> catsList = new ArrayList<>(Arrays.asList(catsArray));(使用方法)}  toArray();

  size();  

1.未解决问题:ArrayList<Cat> cats = new ArrayList<>(); ArrayList<Cat> cats = new ArrayList<Cat>();两者区别

最后再读一篇

0.You cannot simultaneously iterate over a collection and change its elements.(不能同时迭代集合并更改其中的元素,包括插入,删除等操作)

要实现迭代时还要更改其中的内容,就要用到Iterator类

  • hasNext() - returns true or false, depending on whether there is a next item in the list, or we have already reached the last one.
  • next() - returns the next item in the list
  • remove() - removes an item from the list
Iterator<Cat> catIterator = cats.iterator();// Create an iterator
while(catIterator.hasNext()) {// As long as there are elements in the list

   Cat nextCat = catIterator.next();// Get the next element
   System.out.println(nextCat);// Display it
}

关于remove()方法:remove() removes the last element returned by the iterator.

remove()方法的使用:

Iterator<Cat> catIterator = cats.iterator();// Create an iterator
while(catIterator.hasNext()) {// As long as there are elements in the list

   Cat nextCat = catIterator.next();// Get the next element
   if (nextCat.name.equals("Lionel Messi")) {
       catIterator.remove();// Delete the cat with the specified name
   }
}

System.out.println(cats);

 

posted @ 2021-03-22 17:58  吃心王  阅读(159)  评论(0)    收藏  举报