Home  >  Article  >  Java  >  Array and collection operations in Java

Array and collection operations in Java

王林
王林Original
2023-06-08 10:41:471446browse

Array and set operations in Java

As a powerful and popular programming language, Java often needs to use arrays and sets when writing programs. They are one of the most basic and commonly used data structures in the Java language and are used to store, organize and manipulate data. In this article, we will take an in-depth look at array and collection operations in Java.

Array

Array is one of the most basic data structures in the Java language. It is a data structure capable of storing multiple data of the same type. The elements of an array are arranged in a certain order, and the elements are accessed through a numerical index.

In Java, the array is declared as follows:

type[] arrayName;

where type represents the data type of the elements in the array, and arrayName represents the name of the array. For example:

int[] nums; // Define an integer array

When defining an array, you can also assign a value to the array at the same time, for example:

int[] nums = {1, 2, 3, 4, 5}; // Define and initialize an integer array

Access to array elements is through indexing. The index of the array starts from 0, for example:

int[] nums = {1, 2, 3, 4, 5};
System.out.println(nums[0]); / / Output 1

The array length can be obtained through the .length property, for example:

int[] nums = {1, 2, 3, 4, 5};
System.out. println(nums.length); // Output 5

Java also provides some array operation methods. For example, to copy an array, you can use the System.arraycopy() method:

int[] nums = {1, 2, 3, 4, 5};
int[] copyNums = new int[5];
System.arraycopy(nums, 0, copyNums, 0, nums.length); // Copy nums array to copyNums array

Collection

A collection is a container used to store a group of objects in Java. It provides a more complex and flexible data structure than an array. The Java collection framework provides multiple types of collections, such as List, Set, Map, etc. Each collection has different characteristics and applicable scenarios.

List is an ordered collection in which elements can be repeated. Commonly used Lists include ArrayList and LinkedList. The following is an example of ArrayList usage:

List list = new ArrayList<>();
list.add("Java");
list.add("Python") ;
list.add("C ");
System.out.println(list.get(0)); // Output Java

Set is an unordered collection, where The elements cannot be repeated. Commonly used Sets include HashSet and TreeSet. The following is an example of usage of HashSet:

Set set = new HashSet<>();
set.add(1);
set.add(2);
set.add(3);
System.out.println(set.contains(1)); // Output true

Map is a collection of key-value pair mappings, where the elements are Stored as value pairs. Commonly used Maps include HashMap and TreeMap. The following is an example of HashMap usage:

Map map = new HashMap<>();
map.put("Java", 1);
map.put( "Python", 2);
map.put("C ", 3);
System.out.println(map.get("Java")); // Output 1

Collections in Java also provide many operation methods. For example, List can use add, remove, get and other methods to operate elements, Set can use add, remove, contains and other methods to operate elements, and Map can use put, get, remove and other methods to operate keys. value pair.

Conclusion

Arrays and collections are one of the most basic and commonly used data structures in Java. They can store, organize and manipulate data. In Java, arrays and collections have many operation methods to meet the needs of different scenarios. Proficiency in the operations of arrays and sets is one of the basic skills necessary for Java programmers.

The above is the detailed content of Array and collection operations in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn