Get the App
SLTechnology News&Howtos  ›  Development  › 

How to declare and create arrays in Java

Shulou Source: shulou.com Published: 2022-06-01 13:20:26 10月06日 Update

This article mainly introduces how to declare and create an array in Java related knowledge, the content is detailed and easy to understand, the operation is simple and fast, with a certain reference value, I believe you will have something to gain after reading this Java article on how to declare and create an array, let's take a look.

Array

Array is a collection of ordered data. In Java, java.util.Arrays contains various methods for manipulating arrays, such as sorting and searching. All of its methods are static methods, which are very easy to call.

Two methods for declaring arrays

Method 1:

Data type [] array

Method 2:

Data type array []

Two ways to create an array

Method 1:

Data type [] array = new data type [n]

Int [] array = new int [10]

Method 2:

Data type [] arrray = {value1, value2,...}

Int [] array = new int [10]

Indexes

Index can help us locate the desired data and greatly improve the speed of data retrieval.

Custom array generics

Shows a specified data type called generics. E, taken from the initials of Element (element). Where E appears, we can replace it with a reference data type, indicating which reference type of element we will store.

Constructor / / Parametric construction public Array (int capacity) {data = (E []) new Object [capacity]; size = 0;} / / No-parameter construction public Array () {this (10);} element operation / / add element public void addFirst (E element) {/ / if the maximum capacity of the array is exceeded, throw an exception if (size = = data.length) {throw new RuntimeException ("array is full!") } / / list all index and element backward for (int I = size-1; I > = 0; iMub -) {data [I + 1] = data [I];} / / the size of the array is assigned to element data [0] = element / / Array size + 1 size++} / / add element public void addLast (E element) {/ / if the maximum capacity of the array is exceeded, throw the exception if (size = = data.length) {throw new RuntimeException ("array is full!");} / / the size of the array is assigned to element data [size] = element; / / Array size + 1 size++ } / / add element public void add (int index, E element) {/ / if the maximum capacity of the array is exceeded, throw the exception if (size = = data.length) {throw new RuntimeException ("reached max capacity");} if (index)

< 0 || index >

Size) {throw new RuntimeException ("invalid index");} / list all index and subsequent elements move back for (int I = size-1; I > = index; imuri -) {data [I + 1] = data [I];} data [index] = element; size++;} call public static void main (String [] args) {/ / create array Array array = new Array (10) / / add array.addLast (2); array.addLast (3); array.addLast (4); System.out.println (array.toString ()); / / add array.addFirst (1); array.addFirst (0); System.out.println (array.toString ()); / / add elements array.add (0,-1) through index; array.add (6, 5) System.out.println (array.toString ());}

Output result:

Array {data= [2, 3, 4, null, null]}

Array {data= [0, 1, 2, 3, 4, null, null]}

Array {data= [- 1,0,1,2,3,4,5, null, null, null]}

The complete code import java.util.Arrays;public class Array {private E [] data; / / data private int size; / / number of array elements / / Parametric construction public Array (int capacity) {data = (E []) new Object [capacity]; size = 0;} / / No-parameter construction public Array () {this (10) } / / get array capacity public int getCapacity () {return data.length;} / / get the number of array elements public int getSize () {return size;} / / determine whether the array is empty public boolean isEmpty () {return size = = 0 } / / add element public void addFirst (E element) {/ / if the maximum capacity of the array is exceeded, throw the exception if (size = = data.length) {throw new RuntimeException ("array is full!");} / / list all index and element backward for (int I = size-1; I > = 0 Element data -) {data [I + 1] = data [I];} / / the size of the array is assigned to element data [0] = element; / / array size + 1 size++ } / / add element public void addLast (E element) {/ / if the maximum capacity of the array is exceeded, throw the exception if (size = = data.length) {throw new RuntimeException ("array is full!");} / / the size of the array is assigned to element data [size] = element; / / array size + 1 size++ } / / add element public void add (int index, E element) {/ / if the maximum capacity of the array is exceeded, throw the exception if (size = = data.length) {throw new RuntimeException ("reached max capacity");} if (index)

< 0 || index >

Size) {throw new RuntimeException ("invalid index");} / list all index and subsequent elements move back for (int I = size-1; I > = index; imuri -) {data [I + 1] = data [I];} data [index] = element; size++ } @ Override public String toString () {return "Array {" + "data=" + Arrays.toString (data) +'}';} public static void main (String [] args) {/ / create array Array array = new Array (10); / / add array.addLast (2); array.addLast (3) Array.addLast (4); System.out.println (array.toString ()); / / add array.addFirst (1); array.addFirst (0); System.out.println (array.toString ()); / / add elements array.add (0,-1) through index; array.add (6, 5) System.out.println (array.toString ());}} this is the end of the article on how to declare and create arrays in Java. Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to declare and create arrays in Java". If you want to learn more, you are welcome to follow the industry information channel.

Tags: Array element data method type capacity maximum size head tail index backward knowledge two number content article order code value Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Shulou Tech Info NVidia OPPO Reno vpn