Using loops to manipulate arrays in Java
In practical development we often use loops to control the operation of array members. such as: 650) this.width=650; "src=" http://img.mukewang.com/5372da76000146e908070159.jpg "/>
Operation Result: 650) this.width=650; "src=" http://img.mukewang.com/5372da900001155a02500094.jpg "/>
Wherein, 650) this.width=650; "src=" http://img.mukewang.com/5372dab60001c6c601140027.jpg "/> To get the length of the array
"Minor problems" to be aware of:
1, array subscript starting from 0 . So Scores[3], which represents the 4th element in the array, not the 3rd element
2, the array subscript range is 0 to the array length-1 , if the cross-border access, will be error. such as: 650) this.width=650; "src=" http://img.mukewang.com/5372db2c0001ee4303280058.jpg "/>
The runtime will report the following error:
650) this.width=650; "src=" http://img.mukewang.com/5372db510001831408440046.jpg "/>
The above error message means that the array subscript exceeds the range, that is, the array access is out of bounds. In the above code, create an array of length 2, so the array subscript range is 0 to 1, and the program subscript appears 2, that is, scores[2], over the range, resulting in array access.
Code:
public class HelloWorld {
public static void Main (string[] args) {
Defines a string array of length 3 and assigns the initial value
String[] Hobbys = {"Sports", "Game", "movie"};
System.out.println ("The value of the element in the loop output array:");
Iterating through the elements in an array using loops
for (int i=0;iSystem.out.println (Hobbys[i]);
}
}
Operation Result:
The value of the elements in the loop output array:
Sports
Game
Movie
This article is from "Ghost" blog, please make sure to keep this source http://caizi.blog.51cto.com/5234706/1547973
Java Basics---Using loops to manipulate arrays in Java (31)