1. Definition of the array:
An array is an ordered set of data elements of the same type, each of which is called an array element (for short, an element), each element is constrained by n (n≥1) linear relationships, the ordinal of each element in an n linear relationship i1, I2 、...、 in is called the subscript of the element, and the array is called n Dimension array. 2. Features of the array:
The element itself can have a structure that belongs to the same data type, and an array is a set of data with a fixed format and number.
650) this.width=650; "src=" http://img.blog.csdn.net/20160528114808135 "width=" 340 "height=" 207 "style=" border:0px; " />650) this.width=650; "src=" http://img.blog.csdn.net/20160528114833807 "width=" 393 "height=" 191 "style=" border : 0px; "/>
3. Syntax of the array:
Package com.eduask.test;
Import Java.util.Scanner;
public class Arraydemo {
public static void Main (string[] args) {
1, the first kind of grammar
A. Defining an array of type int
int [] s;
int s[];
B. to divide the length of an array
S=new Int[5];
s[0]=51;
System.out.println ("Length of the array:. Length:" +s.length);
c. Dynamic Assignment
for (int i=0;i<s.length;i++) {
System.out.println ("Please enter" + (i+1) + "number:");
S[i]=new Scanner (system.in). Nextint ();
}
System.out.println (S[0]);
System.out.println (S[1]);
System.out.println (s[2]);
System.out.println (S[3]);
System.out.println (S[4]);
System.out.println (S[5]);
2. Static assignment of the second syntax
int [] i={11,22,33,44,55};
int [] ii={11, (int) 2.9};
System.out.println (ii[1]);
3. Less use of the third syntax
int [] ss=new int[]{1,2,3,4,5};
650) this.width=650; "id=" Code_img_closed_5911db17-fe19-407e-864b-ba158d234c26 "class=" code_img_closed "src=" http ://images.cnblogs.com/outliningindicators/contractedblock.gif "style=" border:0px;vertical-align:middle; padding-right:5px; "/>View Code
3. Examples of subject matter reference:
Example 1
Here is a package name
Package com.eduask.test;
This imports the input object scanner
Import Java.util.Scanner;
public class Test01 {
1. Define an array, assign values by keyboard input values, and output the values of the array
public static void Main (string[] args) {
Int[] A = new int[3];//declares an array (length 3)
Instantiating an input object
Scanner sc = new Scanner (system.in);
Iterating through an array
for (int i=0; i<a.length; i++) {
System.out.println ("Please enter" + (i+1) + "number");
int num = Sc.nextint ();//value assigned by keyboard input
A[i] = num;//give this value to the array
}
Print output
for (int i=0; i<a.length; i++) {
System.out.print (a[i]+ "");
}
}
}
Example 2
Package com.eduask.test;
Import Java.util.Scanner;
/**
Use the array to save the data, complete the supermarket small ticket printing
Such as:
Please enter the purchase product: Towel name
Please input unit Price: 25.0 Unit Price
Please enter purchase quantity: 2 quantity
Please enter the purchase product: Basin
Please input unit Price: 15.0
Please enter purchase quantity: 3
====== Welcome * * Supermarket ===========
Product Name Unit Price Quantity Total Price
Towels 25.0 2 50.0
Basin 15.0 3 45.0
Your total consumption: 95.0 yuan
-------------------------------------------------
The name of the product and the unit price are stored in the array, and the user enters the name of the product, only if there is a product on the shelf
Store the item in an array of customer purchases to export the supermarket ticket
Such as:
Please enter product Name: Towel
(If the product is not there)
Sorry, there is no this product
(If the product is available)
Please enter purchase quantity: 4
Cycle.... Until the user exits
====== Welcome * * Supermarket ===========
Product Name Unit Price Quantity Total Price
Towels 25.0 2 50.0
Basin 15.0 3 45.0
Your total consumption: 95.0 yuan
Note: The unit price of the product does not need to be entered, but is obtained by the previously defined data
*
*/
public class Test04 {
public static void Main (string[] args) {
Instantiating an input object
Scanner sc=new Scanner (system.in);
Defining an array of products
String [] names={"towel", "toothbrush", "toothpaste", "basin", "Mineral Water"};
Define price arrays and commodities to correspond
Double [] prices={9.9,18.8,25.5,33.9,15};
Enter the type of product you want to buy
System.out.println ("Please enter the type of goods you need to purchase:");
int Num=sc.nextint ();
int [] Goumai=new int[num];//The array is the subscript for the purchased item
int [] count=new int[num]; This array holds the number of items purchased
Double sum=0.0;//storage Total consumption amount
Through a circular way, to make a purchase of goods
for (int j=0;j<goumai.length;j++) {
System.out.println ("Please enter you want to purchase the" + (j+1) + "class Goods:");
Enter the name of the purchase product
String Name=sc.next ();
int index=-1;//to record the subscript of a commodity
Find out if a product list exists for the item
for (int i=0;i<names.length;i++) {
Determines whether the entered product name is in the product array
if (Name.equals (Names[i])) {
Index=i;//true store subscript false does not store subscript
}
}
Determine if the product is found in the product array
if (index!=-1) {
SYSTEM.OUT.PRINTLN ("The Commodity exists");
Goumai[j]=index; Store the subscript of a product in an array
System.out.println ("Please enter the quantity you want to buy:");
Count[j]=sc.nextint ();//corresponds to the number of stores purchased for the subscript
}else{
SYSTEM.OUT.PRINTLN ("The product does not exist");
j--;//if the purchased item does not exist, the re-purchase of the product is reduced
}
}
SYSTEM.OUT.PRINTLN ("====== Welcome to J18 supermarket ===========");
SYSTEM.OUT.PRINTLN ("Unit price of commodity name \ t Total price");
Loop the purchase of an array, stitching the supermarket small ticket
for (int i=0;i<goumai.length;i++) {
int xiabiao=goumai[i]; Take the subscript of the purchased item
System.out.print (names[xiabiao]+ "\ t");
System.out.print (prices[xiabiao]+ "\ t");
System.out.print (count[i]+ "\ t");
System.out.print (prices[xiabiao]*count[i]+ "\ n");
Sum+=prices[xiabiao]*count[i]; Price accumulation for each type
}
System.out.println ("Your total consumption:" +sum+ "Yuan");
}
}
This article is from the "12157213" blog, please be sure to keep this source http://12167213.blog.51cto.com/12157213/1862043
Java CEO--java Array Basics