In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the Java two-dimensional array case analysis of the relevant knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that everyone after reading this Java two-dimensional array case analysis article will have a harvest, let's take a look.
What is an array?
An Array is an ordered sequence of elements. If you name a limited collection of variables of the same type, the name is the array name. The variables that make up the array are called the components of the array, also known as the elements of the array, and sometimes referred to as the subscript variable / 12713827). The numerical number of each element used for a score group is called a subscript. Array is a form in which several elements of the same type are organized in an orderly form for convenience in programming. These ordered collections of similar data elements are called arrays. An array is a collection used to store multiple data of the same type.
Example (equipment column)
Array, element, and subscript:
For example, when playing Arena of Valor, everyone has to provide equipment, and everyone has their own equipment column. Then this equipment bar is an array, in which the equipment is the element, and the location of the equipment is the subscript. That is to say, each subscript corresponds to a device, and the subscript starts at 0, so the subscript for the first equipment is 0.
Declare array int types
When declaring an array, the length of the array is fixed, and the length of the array is constant. There are two ways of declaring. The first one is directly assigned when declaring. The second declaration does not assign a value, but a fixed length, although not assigned, assigns all elements to 0 by default.
Public class Test {public static void main (String [] args) {/ / declare an array of type int and initialize the assignment int [] a = {1 String 2 3 Ling 4 Jing 5,}; / / declare an array to set the length of the array and initialize it all to 0 String [] b=new int [10];}} type
There is no difference between this and the above, and there are also two methods of declaration.
Public class Test {public static void main (String [] args) {/ / declare an array of type int and initialize the assignment int [] a = {1, bb, "bb", "cc"}; / / declare an array to set the length of the array and initialize it to 0 array [] b=new int [10]; / / declare an array of type String and initialize the assignment String [] d = {"aa", "bb", "cc"} / / declare array of fixed length. Default initialization is 0 String [] c=new String [10];}} array operation traversal array
Traversal array: two methods, for loop and for in loop
For loop, where I put three elements in the array, that is, equipment. Loop output, starting with the subscript 0. Zb.length is the size of this array
Public class Test {public static void main (String [] args) {String [] zb= {"Boot of calmness", "Blade of weeping Blood", "Master of famous Dao"}; for (int I = 0; I
< zb.length; i++) { System.out.println(zb[i]); } }} 结果:For in loop, forget how to use this loop can refer to the previous article, Jindan article has a detailed introduction.
Public class Test {public static void main (String [] args) {String [] zb= {"Boot of calmness", "Blade of weeping Blood", "Master of famous Dao"}; for (String s: zb) {System.out.println (s);}} two-dimensional array
A two-dimensional array is essentially an array with an array as its elements, that is, an array of arrays, the type specifier array name [constant expression] [constant expression]. A two-dimensional array is also called a matrix, and a matrix with equal number of rows and rows is called a square matrix. Symmetric matrix a [I] [j] = a [j] [I], diagonal matrix: there are zero elements outside the main diagonal of n-order square matrix.
A two-dimensional array is an ordinary one-dimensional array in which each element is an one-dimensional array, which is combined into a two-dimensional array.
Continue to use the previous example. At the beginning of each game, one side of the data panel has a default sort (the panel that shows the equipment, the economy). Each person has an equipment bar, which is equivalent to an array. Then there are five equipment bars (one team) on the information panel, and they are arranged in the default order, which is also equivalent to an array. An equipment bar is an element, and the position of the equipment bar is the subscript. But each element in this array is also an array, so the data panel is equivalent to a two-dimensional array.
Declare a two-dimensional array
The method of declaring a two-dimensional array is no different from declaring an array, or there are two cases.
Public class Test {public static void main (String [] args) {/ / declare a two-dimensional array and assign int [] [] a = {{123,456}, {789}}; / / declare a two-dimensional array of fixed size int [] [] ns = new int [3] [5];}}
The above introduction of two-dimensional array with Arena of Valor may still be a bit of a mystery to some readers. Now type out the above example with code.
I can't remember the name of the equipment here. I copied the equipment of the last three people directly.
Public class Test {public static void main (String [] args) {/ / five people, each with three pieces of equipment. String [] [] wzry=new String [5] [3]; / / to buy equipment for the first person is to assign a value to the first array wzry [0] [0] = "gem"; wzry [0] [1] = "blood knife"; wzry [0] [2] = "golden body"; / / to buy equipment for the second person is to assign wzry [1] [0] = "iron sword" to the second array Wzry [1] [1] = "straw sandals"; wzry [1] [2] = "armor"; / / to buy equipment for the third person is to assign a value to the third array wzry [2] [0] = "gem"; wzry [2] [1] = "blood knife"; wzry [2] [2] = "golden body" / / to buy equipment for the fourth person is to assign a value to the fourth array wzry [3] [0] = "gem"; wzry [3] [1] = "blood knife"; wzry [3] [2] = "golden body"; / / to buy equipment for the fifth person is to assign a value to the fifth array wzry [4] [0] = "gem" Wzry [4] [1] = "Blood knife"; wzry [4] [2] = "Golden body";}}
Now let's run it and see what everyone's equipment has.
/ / third person's second equipment System.out.println ("third person's second equipment"); System.out.println (wzry [2] [1]); / / first person's third equipment System.out.println ("first person's third equipment"); System.out.println (wzry [0] [2]) / / full equipment of the fifth person System.out.println ("all equipment of the fifth person"); for (int I = 0; I < 3; iTunes +) {System.out.println (wzry [4] [I]);}
Results:
This is the end of the article on "instance Analysis of Java two-dimensional Array". Thank you for reading! I believe you all have a certain understanding of the knowledge of "Java two-dimensional array case analysis". 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.