In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
Today, the editor will share with you the relevant knowledge points of Java entry array instance analysis, the content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.
Cognitive array
Definition of array
An array is an ordered collection of data of the same type. An array describes several pieces of data of the same type, arranged and combined in a certain order. Where each data is called an element, and each element can be accessed through an index (subscript).
Four basic characteristics of array
The length is fixed. Once an array is created, its size cannot be changed.
The type of its element must be the same type, and mixed types are not allowed.
Array types can be any data type, including base types and reference types.
Array indexed: the index starts at 0 and ends with the array .length-1
Array variables are of reference type, and arrays are also objects.
PS: an array variable is a reference type, an array is also an object, and each element in the array is equivalent to a member variable of that object. The array itself is an object, and the object in Java is in the heap, so whether the array holds the original type or other object types, the array object itself is stored in the heap.
Array subscript starts at 0
Arr [0] = = 12
Arr [1] = = 6
Arr [2] = = 27
...
Arr [5] = = 43
Initialization of array
There are three ways to initialize an array: default initialization, static initialization, and dynamic initialization.
Default initialization
An array is a reference type, and its elements are equivalent to instance variables of the class, so once the array allocates space, each element in it is implicitly initialized in the same way as the instance variables.
Int [] arr = new int [3]; / / the array has default initialization values
Default values for an array of basic data types:
Byte []: 0
Short []: 0
Cahr []:'\ u0000'
Int []: 0
Long []: 0
Float []: 0.0
Double []: 0.0
Boolean []: false
Default value for an array of reference data types: null
Static initialization
In addition to using the new keyword to generate an array, you can directly allocate space and values to the array elements while defining the array.
Int [] arr = {12jie 23pr 45}; int [] arr = new int [] {12je 23pr 45}; dynamic initialization
Array definition is done separately from the operation of allocating space and assigning values to array elements.
Int [] arr; arr = new int [3] arr [0] = 12 position arr [1] = 23 10 arr [2] = 45; traversal of array-related problem arrays
Two methods
Public class TestCode04 {public static void main (String [] args) {int [] arr1 = new int [] {1, 2, 3, 4, 5}; int [] arr2 = {5, 4, 3, 2, 1}; / / Mode 1, the method for (int I = 0; I) to obtain the array length using the ordinary for loop / / arr1.length
< arr1.length; i++) { System.out.print(arr1[i] + " "); } System.out.println(); //方式2,使用增强for循环 for (int a : arr2) { System.out.print(a + " "); } }}求最值问题 问题:给定一个数组int[] arr = {3,5,6,17,26,9,0,7}; ,求出数组中最大的数 public class TestCode05 { public static void main(String[] args) { int[] arr={3,5,6,17,26,9,0,7}; int max=arr[0];//假设最大元素为数组arr[0]位置上的数 //然后依次向后比较 for(int i=1;imax){ max=arr[i]; } } System.out.println(max); }}查询子元素 查询指定位置元素 给定一个数组,查询索引位置为2上的元素 public class TestCode06 { public static void main(String[] args) { int[] arr={3,5,6,17,26,9,0,7}; //直接输出arr[2]; System.out.println(arr[2]); //输出-->6}}
The above code shows one of the advantages of arrays: when querying by location, it is very efficient.
Query the location of the specified element-> find out the index corresponding to the element
Public class TestCode07 {public static void main (String [] args) {/ / query the location of the specified element-- > find out the index corresponding to the element / / give an array: int [] arr = {12men34, 56, 7, 7, 56) / / 0 12 3 4 5 / / function: query the index corresponding to element 12: int index =-1; / / this initial value can be for as long as it is not an array index.
Arrays.copyOf (arr,index); / / replication of a pair of arrays
Arrays.copyOfRange (arr,startindex,endindex); / / interval replication
Arrays.equals (arr1,arr2); / / compare whether the values of the two arrays are the same
Arrays.fill (arr,1); / / Array fill
Import java.util.Arrays;public class TestCode08 {public static void main (String [] args) {/ / given an array: int [] arr = {1 Arrays.toString 3 Arrays.toString 7 2 4 8}; / / toString: traverses the array and returns a string System.out.println (Arrays.toString (arr)) / / binarySearch: dichotomy search: find out the index corresponding to the specified elements in the specified array: / / premise of the use of this method: be sure to check an ordered array: / / sort: sort-- > ascending Arrays.sort (arr) System.out.println (Arrays.toString (arr)); System.out.println (Arrays.binarySearch (arr,4)); int [] arr2 = {1, newArr, 7, 7, 4, 8}; / / copyOf: finish copying the array: int [] newArr = Arrays.copyOf (arr2,4) System.out.println (Arrays.toString (newArr)); / / copyOfRange: interval replication: int [] newArr2 = Arrays.copyOfRange (arr2,1,4); / / [1Med 4)-- > 1Med 2 Jing 3 position System.out.println (Arrays.toString (newArr2)) / / equals: compare whether the values of the two arrays are the same: int [] arr3= [] arr3= 3, 7, 2, 2, 4, 8}; int [] arr4 = {1, 7, 2, 4, 8}; System.out.println (Arrays.equals (arr3,arr4)); / / true System.out.println (arr3==arr4) / / false = = compare whether the values on the left and right sides are equal, compare the address values on the left and right, and return the result must be false / / fill: the filling of the array: int [] arr5 = {1 arr5 = {1, 3, 7, 2, 4, 8}; Arrays.fill (arr5,10); System.out.println (Arrays.toString (arr5));}} two-dimensional array
In essence, all are one-dimensional arrays.
Initial mode of two-dimensional array
Static initialization
Int [] [] arr = {{1 arr 2}, {4 arr = {{1 arr 2}, {4 arr = {{1 recorder 2}, {4 rector 5 recorder 6}, {4 recorder 5 parade 6}, {4 pyrrine 5 parade 6 pencore 8 parentin 9}}
Dynamic initialization
Int [] [] arr = new int [3] []; / / essentially defines that the length of one-dimensional array is 3, and each "grid" is filled with an array arr [0] = new int [] {1pr 2}; arr [1] = new int [] {3pr 4pm 5pm 6}; arr [2] = new int [] {34PM 455e 56}
Default initialization
An array is a reference type, and its elements are equivalent to instance variables of the class, so once the array allocates space, each element in it is implicitly initialized in the same way as the instance variables.
Int [] [] arr=new int [3] [3]; / / defines a 2-D array of 3'3. The default value is 0 2-D array traversal.
Two for loops
For (int I = 0; I < arr.length; iTunes +) {for (int j = 0; I < arr [I] .length; jacks +) {System.out.print (ARR [I] [j] + ");} System.out.println () / / Line break}} above is all the content of the article "Java getting started Array instance Analysis". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to 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.