Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use ArrayList in Java

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to use ArrayList in Java". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to use ArrayList in Java.

Overview of 1.ArrayList class 1.1ArrayList class

In java, we will often use collection, collection is the most important module in our Java SE, when we create a List collection, we often use new ArrayList (); therefore, this is particularly important, this article will give you a detailed description of the relevant knowledge points, and will be illustrated by a large number of cases.

What is a collection?

A storage model with variable storage space is provided, and the stored data capacity can be changed.

The characteristics of ArrayList sets

The underlying layer is implemented by an array, and the length can be changed.

The use of generics

Used to constrain the data type of the element stored in the collection

Common methods of 1.2ArrayList class

We can check the usage and explanation of ArryList through the API we learned in the previous article, which is a good opportunity to practice using API.

1.2.1 Construction method

Method name

Description

Public ArrayList ()

Create an empty collection object

1.2.2 member method

Method name

Description

Public boolean remove (Object o)

Deletes the specified element and returns whether the deletion was successful

Public E remove (int index)

Deletes the element at the specified index and returns the deleted element

Public E set (int index,E element)

Modifies the element at the specified index and returns the modified element

Public E get (int index)

Returns the element at the specified index

Public int size ()

Returns the number of elements in the collection

Public boolean add (E e)

Appends the specified element to the end of this collection

Public void add (int index,E element)

Inserts the specified element at the specified location in this collection

1.2.3 sample code public class ArrayListDemo02 {public static void main (String [] args) {/ / create collection ArrayList array = new ArrayList (); / / add elements array.add ("hello"); array.add ("51CTO"); array.add ("one count"); / / public boolean remove (Object o): delete the specified element and return whether the deletion was successful / / System.out.println ("world") / / System.out.println (array.remove ("javaee")); / / public E remove (int index): delete the element at the specified index and return the deleted element / / System.out.println (array.remove (1)); / / IndexOutOfBoundsException// System.out.println (array.remove (3)) / / public E set (int index,E element): modifies the element at the specified index and returns the modified element / / System.out.println (array.set (1, "javaee")); / / IndexOutOfBoundsException// System.out.println (array.set (3, "javaee")); / / public E get (int index): returns the element at the specified index / / System.out.println (array.get (0)); / / System.out.println (array.get (1)) / / System.out.println (array.get (2)); / / System.out.println (array.get (3)); / / public int size (): returns the number of elements in the collection System.out.println (array.size ()); / / outputs the set System.out.println ("array:" + array);}}

Those that have been deleted need to be tested by yourselves, leaving you with a small opportunity to put it into practice. now the implementation results are as follows:

1.3ArrayList stores strings and traverses 1.3.1 case requirements

Create a collection of stored strings, store 3 string elements, and use the program implementation to traverse the collection in the console.

According to this demand, we give the following ideas to solve the problem:

1: create a collection object

2: add a string object to the collection

3: to traverse the collection, you must first be able to get every element in the collection, which is realized by the get (int index) method

4: traverse the collection, and then be able to get the length of the collection, which is achieved by the size () method

5: general format for traversing collections

1.3.2 Code implementation

According to this requirement and idea, we give the following implementation code:

Public class ArrayListTest01 {public static void main (String [] args) {/ / create a collection object ArrayList array = new ArrayList (); / / add a string object array.add ("the length of a count"); array.add ("left cold meditation"); array.add ("the wind is clear and clear"); / / traverse the collection, and then be able to get the length of the collection, which is implemented by the size () method / / System.out.println (array.size ()). / / the common format for traversing collections, for (int item0; I)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report