search
HomeJavaJavaBaseThree traversal methods of java collection ArrayList

Three traversal methods of java collection ArrayList

Nov 30, 2019 pm 03:56 PM
arraylistjavaTraversegather

Three traversal methods of java collection ArrayList

ArrayList

ArrayList uses continuous memory units to store data elements and is a dynamic array whose capacity can grow dynamically.

When a data element is added or removed (except for the last position), ArrayList needs to move all elements behind the added (or removed) element. So inserting and deleting elements is slower, and querying is faster.

At the same time, the ArrayList thread is unsafe! Generally, ArrayList is used in single threads, while Vector and CopyOnWriteArrayList are generally used in multi-threads.

Recommended java related video tutorials: java online learning

Note:

1, ArrayList has been covered tostring can print the results directly.

2. toArray() will convert elements into Object type.

ArrayList traversal methods

ArrayList has three traversal methods, namely:

1. Iterator traversal

Iterator<Integer> it = arrayList.iterator();
while(it.hasNext()){
    System.out.print(it.next() + " ");
}

2. Index value traversal

for(int i = 0; i < arrayList.size(); i++){
   System.out.print(arrayList.get(i) + " ");
}

3. for loop traversal

for(Integer number : arrayList){
   System.out.print(number + " ");
}

Note: It should be noted that when traversing ArrayList, traversal through index values ​​is the most efficient, followed by for loop traversal, and iterator traversal is the lowest.

More related articles and tutorials are recommended: Java language introduction

The above is the detailed content of Three traversal methods of java collection ArrayList. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:CSDN. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment