Java: Eighth chapter

Source: Internet
Author: User

Eighth Chapter Array

     1. Declaration Definitions for Arrays
data type [] Variable name = new data type [length];
Column: Int[]ary = new Int[5];
2. Value, Assignment
Value: Data name [subscript];
Column: int a = ary[1];
Assignment: variable = data name [subscript];
Column: ary[1]=10;
3. Iterating the array
The length of the array: array name. length;
for (int i=0;i< array name. length;i++) {
Array name [i]: Access value for each element
}
4. Common Exceptions for arrays
Array subscript out of bounds
ArrayIndexOutOfBoundsException
When the subscript of an array is accessed beyond 0~length-1, the
The above error will occur.
Note: array subscript range: 0~length-1
5. Common methods of arrays
Arrays.tostring (array name);//Display array contents
Arrays.sort (array name); Array in ascending order
6. Post-traversal
for (int i = ary.length-1;i>=0;i--) {
Ary[i]
}
7. Compare the size of a string
If A.compareignorecase (b) >0 is true,
So a>b.
If A.compareignorecase (b) <0 is true,
So a<b.
If A.compareignorecase (b) =0 is true,
So a=b.
8.break and Continue
Break: Terminate, End (indicates terminating the current loop structure)
Continue: Continue (indicates the end of this cycle, into the next cycle)
Note: Multi-layer loops only work on direct loops.

Example: Sorting an array after an inverse order Public Static voidMain (string[] args) {int[] AA =New int[]{1,3,-1,5,-2}; System. out. Print ("the original array is:");  for(intI=0; i<aa.length;i++) {System. out. Print (aa[i]+"\ t"); } System. out. println (""); System. out. Print ("the following arrays are processed in reverse order:");  for(intj = aa.length-1; j>=0; j--){            if(aa[j]<0) {Aa[j]=0; }system. out. Print ((aa[j]+"\ t")); }} Example: Inserting a value into an array Public Static voidMain (string[] args) {int[] ary =New int[6]; ary[0] = -; ary[1] = the; ary[2] = the; ary[3] = -; ary[4] = About; intindex =0; Scanner Console=NewScanner (System.inch); System. out. println ("Please enter a number:"); intnum =Console.nextint ();  for(inti =0; i<ary.length;i++){            if(ary[i]>num) {Index=i; System. out. println (Ary[i]);  Break; }        }         for(inti =4; i>=index;i--) {Ary[i+1] =Ary[i]; } Ary[index]=num; System. out. println (arrays.tostring (ary)); }

Java: Eighth chapter

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.