In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
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.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.