Introduction and use of java Array
This article introduces the relevant knowledge of "introduction and use of java array". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Introduction
Arrays are one of the most important data structures for every programming language. It refers to a set of related types of variables, and these variables operate in a uniform way, using a fixed size of storage space to store data.
How the array is declared
DataType [] arrayRefVar; / / preferred method
Or
DataType arrayRefVar []; / / the effect is the same, but not the preferred method
How the array is created
ArrayRefVar = new dataType [arraySize]
Or
DataType [] arrayRefVar = {value0, value1,..., valuek}
Operation of array
The array type and size are determined, so when dealing with array elements, you can operate through the for loop or through the array subscript; (note: array subscript starts at 0)
Examples are as follows:
Public class TestArray {
Public static void main (String [] args) {
Double [] myList = {1.9,2.9,3.4,3.5}
/ / print all array elements
For (int I = 0; I
< myList.length; i++) { System.out.println(myList[i] + " "); } // 计算所有元素的总和 double total = 0; for (int i = 0; i < myList.length; i++) { total += myList[i]; } System.out.println("Total is " + total); // 查找最大元素 double max = myList[0]; for (int i = 1; i < myList.length; i++) { if (myList[i] >Max) max = myList [I]
}
System.out.println ("Max is" + max)
}
}
Multidimensional array
A multidimensional array is equivalent to an array that is an array. For example, a two-dimensional array is a special one-dimensional array, and each element is an array, as shown below:
String str [] [] = new String [3] [4]
The syntax of the two-dimensional array is as follows:
Type [] [] typeName = new type [typeLength2] [typeLength3]
Note: multi-dimensional array is the same.
This is the end of the introduction and use of the java array. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!