Java preliminary cognition and use of one-dimensional arrays

Source: Internet
Author: User

Why do you use arrays
When you set too many variables of the same type, we use the array variable for ease of operation and management.
He's actually a bunch of data in an orderly place.
What is an array
1. Array declarations
Identifier
Array elements
Element subscript
Element type
element type identifier [element subscript, starting from 0]=
{array elements, array elements, array elements (array length, currently 3)};

Or
element type [element subscript, starting from 0] Identifier =
{array elements, array elements, array elements (array length, currently 3)};

Example 1:
int scores[]={1,2,3}; Array length 3
Int[] scores={1,2,3}; Array length 3
String scores[]={"ASD", "ASD", "ASD", "ASD", "ASD"}; Array length 5
String[] scores={"ASD", "ASD", "ASD", "ASD", "ASD"}; Array length 5

Example 2:
int scores[]=new int[10]; Array length 10
Int[] Scores=new int[10]; Array length 10
String Scores[]=new string[20]; Array length 20
String[] Scores=new string[20]; Array length 20

Note 1: The same array can only use one of the declarations in Example 1 and Example 2.
For example, int scores[]={1,2,3}; int scores[]=new int[10]; this is wrong.
Note 2: At the time of the declaration, the array length and the tuple can only write one.
For example, int scores[3]={1,2,3}; int scores[]=new int[3] ={1,2,3}; this is wrong.

2. Assigning values to arrays
The array's subscript must be written clearly when assigning values to the array.
For example
int scores[]=new int[10];
scores[0]=5;
scores[1]=5;
Scores[0]= scores[0]*5;
System.out.println (scores[0]+ "\ T" + scores[1]);

The result is: 25 5

A common use
Add a keyboard assignment operation with a for loop
Scanner Input=new scanner (system.in);
int scores[]=new scores[10];
int num=0;
for (int i=0;i< scores.length;i++) {//Loop 10-time assignment
Scores[i]=input.nextint (); Assigning a value with a keyboard
num+= Scores[i]; Finding the sum of array data
}
Tip 1:scores.length equals the length of the array can be used directly to determine the number of loops.

With the enhanced for loop
Scanner Input=new scanner (system.in);
int scores[]=new scores[10];
int num=0;
for (int score:scores) {//Loop 10-time assignment
num+= score; Finding the sum of array data
}
Tip: for (int score:scores) array-specific hardening for statement, which means that each loop will scores the value of the array from the subscript "0" and assign the value to score.

Common Mistakes
1. Whenever a declaration is written with an assignment and an array length, the assignment and array length must be written one.
int scores=new scores[]; Error
int scores=new scores[1]; Correct
int Scores=new scores[]{1,2,3}; Correct

2. Array out of bounds
int scores=new scores[2];
Scores[0]=1;
Scores[1]=1;
Scores[2]=1;
Scores[3]=1;
Analysis: The array declaration of two data, so should not appear scores[2] and scores[3], this is an array out of bounds.

3 syntax rules, declarations and arrays all-in-one assignment must be completed with a single statement
int scores[];
scores={1,2,3}; Error

int scores[]={1,2,3}; Correct

Java preliminary cognition and use of one-dimensional arrays

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.