Array, is a group of numbers!!!
Before we learned the variable can only save one data, if a department has 50 employees of the name to save, how to do? If you define a 50 variable
String name1;
String name2;
......
......
String name49;
String Name50;
, this picture is too beautiful, I dare not look ... 650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0004.gif "alt=" J_0004.gif "/>
Even if you save it, now I want to find a "Luo Hammer" staff, the code how to write it?
if (name1.equals ("Luo Sledgehammer")) {
}
if (name2.equals ("Luo Sledgehammer")) {
}
......
if (name49.equals ("Luo Sledgehammer")) {
}
if (name50.equals ("Luo Sledgehammer")) {
}
, this code is more beautiful, see to swallow saliva ... 650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0008.gif "alt=" J_0008.gif "/>
At this point, the array appeared!!! He is wearing a golden armor, the foot of the colorful clouds ........., okay, no nonsense, directly on the code:
public static void main (String[] args) { //define a String[] to save 50 employee names NAMES&NBSP;=&NBSP;NEW&NBSP;STRING[50];&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;//2. Assigning Values names[0] = "Zhang Xiao Meng"; names[10] = "Li Yi-huan"; names[20] = "Luo Sledgehammer"; //3. Find out if an employee is called Rhoda for (int i = 0; i < names.length; i++) { if (Names[i]!=null && names[i]. Equals ("Luo sledgehammer")) { system.out.println ("Find the sledgehammer, his number is"+i); break; } } }
Attention:
-
The benefit of the array is that it solves the storage problem of the same type variable.
-
The definition array is more than a pair of [], and the default value of the array, the value of the numeric type is 0, and the value of the reference type is null, compared to defining the normal variable.
-
Later new STRING[50], indicating that the array can store the number of data, if the use of more than the scope of the error.
-
Assignment is names[0] = "xxx", where [0] means subscript (also called offset). Because the array in memory is a contiguous memory space, so it is such a drop 650) this.width=650; "Src=" https://s2.51cto.com/wyfs02/M01/A3/04/ Wkiol1mj3ktt2cieaaae8hjed9u469.png "title=" 21.png "alt=" Wkiol1mj3ktt2cieaaae8hjed9u469.png "/>,names by default points to the first data, So it offsets 0 positions is the first data, the offset of one position is the second data. So the last data subscript is--49!!!
-
Arrays are typically used in conjunction with loops.
OK, the array is like this, a lot of simple things ah!!! Of course, he has a lot of details of the syntax, such as the definition of this is the correct way to write:
string[] names = new STRING[50]; String[] Names2 = {"AAAA", "BBB", "CCC"}; string[] Names3 = new string[]{"AAAA", "BBB", "CCC"};
Finally, finally, several common operations of arrays:
One, the traversal of the array elements
public static void Main (string[] args) {//define array and assign initial value int[] Nums = {234,64,13,2,7,6,4,334,34}; Iterate through the array, length indicates the number of elements for the array for (int i = 0; i < nums.length; i++) {System.out.println ("The Value of" + (i+1) + "element is:" +nums[i]); } }
Second, the sorting of the array
public static void main (String[] args) { //defines an array and assigns an initial value int[] nums = {234,64,13,2,7,6,4,334,34}; //traversing Arrays for (int i = 0; i < nums.length; i++) { system.out.println ("First" + (i+1) + " The value of the element is: "+nums[i]); } //Array Sort arrays.sort (nums); &NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN ("sorted result:"); //traversal array for (int i = 0; i < nums.length; i++) { system.out.println ("The value of the" + (i+1) + "element is:" +nums [i]); } }
Results:
650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M02/A3/0C/wKioL1mj3znBApwrAABDDvKpujI147.png "title=" 22. PNG "alt=" Wkiol1mj3znbapwraabddvkpuji147.png "/>
If we want to sort in descending order, we can also write our own code:
public static void main (String[] args) { //defines an array and assigns an initial value int[] nums = {234,64,13,2,7,6,4,334,34}; //traversing Arrays for (int i = 0; i < nums.length; i++) { system.out.println ("First" + (i+1) + " The value of the element is: "+nums[i]); } //array sorting for (int i = 0; i < nums.length; i++) { for (int j = i+1; j < nums.length; j++) { if (Nums[i]<nums[j]) { int tmp = nums[i]; nums[i] = nums[j]; nums[j] = tmp; } } } &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN ("sorted result:"); // Traversal array for (int i = 0; i < nums.length; i++) { system.out.println ("+ (i+1) +" The value of each element is: "+nums[i]); } }
Three or two-D arrays
The previous one-dimensional array, such as saving 50 employees in a department, can use a one-dimensional array, if the company has 4 departments, the largest number of departments have 50 employees, it is necessary to define the array:
string[][] names = new STRING[4][50];
And now we're going to use a nested loop to traverse it:
The number of external traversal departments for (int i = 0; i < names.length; i++) {////inner-traversal of employee number for each department for (int j = 0; J < Names[i].length, J + +) { System.out.println ("This is the department" + (I+1) + "employee" + (j+1) + "employee"); }}
Here, the array is really dripping over, the usual, lay out a few exercises:
-
Enter a string that counts the number of capitals, lowercase letters, numbers, and other characters in English.
-
Enter the salary of 10 employees to find out the average salary and total salary, and how many people are below 5000, and how many people 5000 and above
-
There is already an ordered array {23,34,55,87,108,210}, and now enter a number that requires it to be inserted into the array in the same order as it was originally ordered
-
Randomly input 5 integers into an array, then sort from small to large, and output
"Software Thinking" blog address:51CTO,Blog Park, interested small partners can go to see the relevant other blog posts.
This article is from the "Software Thinking" blog, please be sure to keep this source http://softi.blog.51cto.com/13093971/1960246
"Java from getting started to giving up" Javase article: Arrays