In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces the example analysis of Java object-oriented array, which is very detailed and has certain reference value. Friends who are interested must finish it!
An array of Java face objects
1. One-dimensional array
1. Description of the array: a combination of the same type of data.
Description: the ① array is a reference data type, and the elements of the array can be basic data types or reference data types.
two。 Declaration and initialization of one-dimensional array
① dynamic initialization: array declaration and initialization are performed separately
Int [] numbers = new int [5]
② static initialization: array declaration and initialization occur at the same time
Int [] numbers = new int [] {1pm 2pm 3pm 4}
Int [] numbers = {1pm 2pm 3pm 4}
Note: the length of the array is immutable once the array is created, either dynamically or statically.
3. Reference to one-dimensional array elements: subscript (index value)-subscript is the length from 0 to the array-1
Int [] numbers = new int [2]
Numbers [0] = 1; / / assignment
System.out.println (numbers [0]); / / get the value of the element
4. Properties of the array: length- represents the length of the array
Int [] numbers = new int [2]
Int l = numbers.length
5. Traversal of one-dimensional array
Int [] numbers = new int [2]
For (int I = 0; I < numbers.length; iTunes +) {
System.out.println (numbers [I])
}
6. Default initialization values for one-dimensional array elements: the same as the default values for basic and reference data types.
7. Memory structure of one-dimensional array
Painted by ps:Excel, average level, make do with it.
two。 Two-dimensional array
1. How to understand two-dimensional array? The elements in a two-dimensional array are one-dimensional arrays. (elements in an array or an array)
Description: elements of a two-dimensional array are reference data types
two。 Declaration and initialization of two-dimensional array
/ / dynamic initialization
String [] [] persons = new String [2] [3]
String [] persons = new String [2] []
Persons [0] = new String [2]
Persons [1] = new String [5]
/ / static initialization
String [] [] persons = new String [] [] {{"a", "110"}, {" b "," 120"}}
String [] [] persons = {{"a", "110"}, {" b "," 120th "}}
3. How to reference a two-dimensional array element: get the element at a location in the array by using the subscript
String [] [] persons = {{"a", "110"}, {" b "," 120th "}}
System.out.println (persons [0] [0])
4. Property of 2D array: length
String [] [] persons = {{"a", "110"}, {" b "," 120th "}}
Length of persons.length; / / two-dimensional array
Persons [0] .length; / / the length of the element at position 0 in the two-dimensional array
5. Traverse two-dimensional array elements:
String [] [] persons = {{"a", "110"}, {" b "," 120th "}}
For (int I = 0; I < persons.length; iTunes +) {
For (int j = 0; j < persons [I] .length; jacks +) {
System.out.println (persons [I] [j])
}
}
6. Default initialization values for two-dimensional array elements:
The default value for the elements of the ① 2D array is null
The default values and basic data types of the elements of the ② two-dimensional array are the same as the reference data types.
7. Memory structure of two-dimensional array
3.Arrays utility class
Because under the lang package, there is no need for the guide package to call the
Int [] numbers = {12pm 38pm 1je 2je 3je 4je 5je 6}
Int [] numbers2 = {12pm 38pm 1je 2je 3je 4je 5}
/ / determine whether two arrays are equal (the value and number of elements)
Boolean equals = Arrays.equals (numbers, numbers2)
System.out.println (equals)
/ / toString (): outputs the elements in the array
String str = Arrays.toString (numbers)
System.out.println (str)
/ / fill (int [] njinint val): change the value of the array element to val
Arrays.fill (numbers, 100)
Str = Arrays.toString (numbers)
System.out.println (str)
/ / sort (int [] numbers)
Arrays.sort (numbers2)
System.out.println (Arrays.toString (numbers2))
/ / dichotomy search: premises must be sorted. If you can't find a return-1
Int index = Arrays.binarySearch (numbers2, 1)
System.out.println (index)
4. Common anomalies
Int [] numbers = new int [2]
/ *
*
* ArrayIndexOutOfBoundsException
, /
/ / numbers [2] = 5; the subscript is out of bounds
/ / numbers [- 1] = 10
/ / int [] [] n = new int [2] []
/ / n [2] = new int [10]
/ *
* NullPointerException
, /
String [] str = {"aa", "bb"}
/ / String string = str [0] .toUpperCase ()
/ / System.out.println (string)
Str = new String [2]
/ / System.out.println (str [0] .toString ())
5. Deformable parameter
* deformed parameters:
*
* format: public void add (int... Numbers {}
*
* description:
* 1. Deformable parameters of the same type and arrays of the same type are not overloaded (the type is the same as the number)
* 2. Deformable parameters can only be placed on the last one in the formal parameter list.
* 3. There can be only one deformable parameter in a method
* 4. The number of deformable parameters can be 0 or 1 or more
The above is all the content of the article "sample Analysis of Java object-oriented Array". Thank you for reading! Hope to share the content to help you, more related knowledge, 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.