Java Basics---Using loops to manipulate arrays in Java (31)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.