知识库 知识库
首页
  • Hyperskill - Java

    • Java basic
    • Java OOP
    • 应知
    • 扩展
    • IO & Stream
    • Error & Exception
    • Algorithm & Data structure
    • Design pattern
    • Web
    • Spring boot
  • 练习题

    • 选择题 & 填空题
    • 代码题
  • Frank - Java与生活 (OOP)

    • 参考资料
    • Java基础
    • OOP上半部分
    • OOP下半部分
  • Frank - Java API进阶

    • Base API
    • Unit Test and main function
  • 学习笔记
  • 学习笔记

    • 数据库
  • Frank - MySQL删库跑路

    • 安装、连接、配置
    • 基本操作——数据库
    • 基本操作——表
    • 基本操作——数据
    • 数据类型
    • 列属性完整性
    • 数据库设计思维
    • 单表查询
    • 多表查询
  • 学习笔记

    • 其它
  • Frank - Linux现代方法

    • 必知
    • 命令
    • 技巧
  • 技术文档
  • Git
  • GitHub技巧
  • 前端
  • Khan Academy - 语法
  • Monthly
  • 阅读
  • Others
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
收藏
  • 标签
  • 归档
GitHub (opens new window)

Jim FuckPPT

Java小学生
首页
  • Hyperskill - Java

    • Java basic
    • Java OOP
    • 应知
    • 扩展
    • IO & Stream
    • Error & Exception
    • Algorithm & Data structure
    • Design pattern
    • Web
    • Spring boot
  • 练习题

    • 选择题 & 填空题
    • 代码题
  • Frank - Java与生活 (OOP)

    • 参考资料
    • Java基础
    • OOP上半部分
    • OOP下半部分
  • Frank - Java API进阶

    • Base API
    • Unit Test and main function
  • 学习笔记
  • 学习笔记

    • 数据库
  • Frank - MySQL删库跑路

    • 安装、连接、配置
    • 基本操作——数据库
    • 基本操作——表
    • 基本操作——数据
    • 数据类型
    • 列属性完整性
    • 数据库设计思维
    • 单表查询
    • 多表查询
  • 学习笔记

    • 其它
  • Frank - Linux现代方法

    • 必知
    • 命令
    • 技巧
  • 技术文档
  • Git
  • GitHub技巧
  • 前端
  • Khan Academy - 语法
  • Monthly
  • 阅读
  • Others
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
收藏
  • 标签
  • 归档
GitHub (opens new window)
  • Hyperskill - Java

    • Java basic

    • Java OOP

    • 应知

      • Theory:Functional decomposition
      • Theory:Paradigms
      • Theory:Overloading
      • Theory:Write, compile, and run
      • Theory:Annotations basics
      • Theory:JVM, JRE, and JDK
      • Theory:Jave Archive
      • Theory:Running programs on your computer
      • Theory:Enums in Java
      • Theory:Fields and methods in enum
      • Theory:StringBuilder
      • Theory:Immutability
      • Theory:Boxing and unboxing
      • Theory:Introduction to generic programming
      • Theory:Generics and Object
      • Theory:What are collections
      • Theory:The collections Framework overview
      • Theory:ArrayList
        • Resizable arrays
        • Creating an instance of ArrayList
        • Basic methods
        • More ArrayList methods
        • Iterating over ArrayList
        • Conclusion
      • Theory:The List interface
      • Theory:Comparable
      • Theory:Processing strings
      • Theory:Initialization blocks
      • Theory:Introduction to API
      • Theory:Generic methods
    • 扩展

    • IO & Stream

    • Error & Exception

    • Algorithm & Data structure

    • Design pattern

    • Web

    • Spring boot

  • 练习题

  • Frank - Java与生活

  • Frank - Java API进阶

  • 学习笔记

  • Java
  • Hyperskill - Java
  • 应知
Jim
2022-10-04
目录

Theory:ArrayList

# Resizable arrays

One of the most widely used classes of Java Class Library is a class named ArrayList that represents a resizable array of objects of a specified type. Unlike the standard array denoted as [], it can dynamically grow after the addition and shrink after the removal of its elements. This behavior is very useful if you do not know the size of the array in advance or you need one that can change size over the lifetime of a program.

In fact, this class is built on top of a standard Java array, extending it with a set of convenient operations. Like a standard array, it allows getting the current number of elements (its size) as well as accessing its elements by their indexes.

There is only one restriction: ArrayList: being a generic class, it cannot store primitive types. However, it can store any reference types, including String's, wrapper classes (like Integer's), other ArrayList's, and custom classes.

# Creating an instance of ArrayList

To start using the class by its short name, make the following import:

import java.util.ArrayList;
1

Let's consider several ways to create instances of this class.

\1) The simplest way is to use a no-argument constructor:

ArrayList<String> list = new ArrayList<>();
1

The created list is empty, but its initial capacity is 10 (by default).

\2) We can also specify the initial capacity of it:

ArrayList<String> list = new ArrayList<>(50);
1

This list is empty, but its initial capacity is set to 50.

\3) Or you can construct an ArrayList that consists of elements of another list:

ArrayList<String> list = new ArrayList<>(anotherList);
1

Regardless of how you create an instance of ArrayList, its size will dynamically change. In this lesson, we will create a list with the default capacity like in the first example.

提示

If you are an advanced user, you know that it is better to create and use an ArrayList via its List interface. We will do it in the next lessons after learning inheritance. We believe that the current approach is enough for now since it requires less knowledge to start using dynamic collections.

# Basic methods

The collection has a set of convenient methods that emulate and extend the functionality of standard arrays. Let's discuss what they are. First, let's initialize some collection:

ArrayList<String> names = new ArrayList<>(); // empty collection of strings
1

First of all, there's a method to determine the size of the collection size that returns the number of elements of the list. Let's try learning the size of ours:

System.out.println(names.size()); // 0
1

As expected, it is empty and the result is zero. We also might want to learn the value of the specified position of the object. For that, collections have a get(int index) method that returns the object of the list which is present at the specified index.

Next, there are a bunch of methods to add elements and set values of a collection:

  • add(Object o) adds a passed element to the last position of the collection;
  • add(int index, Object o) adds a passed element to the specified position of the collection;
  • set(int index, Object o) replaces the element present at the specified index with the object;

Let's add some names to our collection:

names.add("Justin");      // [Justin]
names.add("Helen");       // [Justin, Helen]
names.add(1, "Joshua");   // [Justin, Joshua, Helen]
names.add(0, "Laura");    // [Laura, Justin, Joshua, Helen]
1
2
3
4

And replace one name with another:

names.set(3, "Marie"); // now: [Laura, Justin, Joshua, Marie]
1

We can check that everything is as expected:

System.out.println(names);        // [Laura, Justin, Joshua, Marie]
System.out.println(names.size()); // 4
System.out.println(names.get(0)); // the first element is "Laura"
System.out.println(names.get(3)); // the last element is "Marie"
1
2
3
4

Finally, there are methods for removing elements from the collection:

  • remove(Object o) removes the first occurrence of the specified element from this list, if it is present;
  • remove(int index) removes the element at the specified position in this list;
  • clear() removes all elements from the collection.

Let's try removing elements by value and index:

names.remove("Justin"); // [Laura, Joshua, Marie]
names.remove(1);        // [Laura, Marie]
names.clear();          // []
1
2
3

注意

Important: indexes of elements start with 0 just like for standard arrays

Try to play with this code by yourself and enjoy the power of ArrayList .

# More ArrayList methods

We've illustrated the possibilities of basic methods for collections in Java applied to an ArrayList object. But this class has some more methods of its own. First, let's create another ArrayList:

/* an ArrayList of Integers, not ints */
ArrayList<Integer> numbers = new ArrayList<>();

numbers.add(1);
numbers.add(2);
numbers.add(3);
numbers.add(1);
1
2
3
4
5
6
7

There's also an addAll(Collection c) method for adding the whole collection to an ArrayList. It appends elements of the provided collection to the end of the list:

ArrayList<Integer> numbers2 = new ArrayList<>();    // creating another list of Integers
numbers2.add(100);
numbers2.addAll(numbers); // [100, 1, 2, 3, 1]
1
2
3

The class also has a method called contains that checks whether a list contains a value or not, and two methods indexOfandlastIndexOf that find the index of the first and the last occurrences of an element, respectively. They return -1 if there is no such element.

Let's see:

System.out.println(numbers2.contains(2));    // true
System.out.println(numbers2.contains(4));    // false
System.out.println(numbers2.indexOf(1));     // 1
System.out.println(numbers2.lastIndexOf(1)); // 4
System.out.println(numbers2.lastIndexOf(4)); // -1
1
2
3
4
5

As you see, this class provides a rich set of methods to work with elements. You do not have to write them by yourself, as you do for standard arrays.

# Iterating over ArrayList

It is possible to iterate over elements of an instance of the class. It is done in the same way as iterating over an array. In the following example, we use for and for-each loops to add the five first powers of ten in a list and then print the numbers to the standard output.

ArrayList<Long> powersOfTen = new ArrayList<>();

int count = 5;
for (int i = 0; i < count; i++) {
    long power = (long) Math.pow(10, i);
    powersOfTen.add(power);
}

for (Long value : powersOfTen) {
    System.out.print(value + " ");
}
1
2
3
4
5
6
7
8
9
10
11

The code prints the following:

1 10 100 1000 10000 
1

It is not harder than using a standard array.

# Conclusion

We've considered the ArrayList class from the java.util package. This class is similar to standard Java arrays but has the possibility to dynamically change its size. It has methods to get the size, add, remove and access elements by their indexes. In addition, ArrayList provides a set of useful methods that check whether an element is present in the array and find it. A regular array does not have such methods built-in.

编辑 (opens new window)
#Java#Collection
上次更新: 2022/10/12, 17:01:25
Theory:The collections Framework overview
Theory:The List interface

← Theory:The collections Framework overview Theory:The List interface→

最近更新
01
《挪威的森林》
04-14
02
青钢影
04-14
03
Processing strings
02-18
更多文章>
Theme by Vdoing | Copyright © 2022-2023 Jim Frank | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式