An array of Java foundations

Source: Internet
Author: User
Tags array length

1, the definition of the array

An array is an ordered collection of the same data type, which is a special type of variable that is a contiguous amount of storage space that is opened in memory.

Elements of an array: the specific values that are saved in the array

The length of the array: the maximum number of elements that can be saved in an array.

Subscript for array: Array is the position of the element in the array by subscript, subscript starting from 0, to array length-1

Note: The type of the element in the array must be the same as the data type when the array is declared.

2. Declaration, initialization, assignment of arrays

When an array is declared, the computer does not allocate memory space. Example: int[] nums; or int nums[]; both declarations can be

Nums=new Int[5]; This action allocates memory space for an array.

If an element in an array is not assigned a value, its default initial value differs depending on the data type. int data type default initial value is 0, double data type default initial value is 0.0, boolean data type default initial value is False

Note: The length of the array cannot be changed once it has been declared

The data type of an element in an array cannot be stored in a mix

Mode 1:

Int[] Nums;

Nums=new Int[5];

Nums[0]=1;

nums[1]=2;

.........

Mode 2:

Int[] Nums=new int[5];

News[0]=1;

news[1]=2;

........

Mode 3: int[] nums={10,20,30,40,50};

Mode 4:int[] Nums=new int[]{10,20,30,40,50};

Error mode 1:

Int[] Nums;

NUMS={10,20,30,40,50};

Error mode 2:int[5] nums={10,20,30,40,50};

Error mode 3:int[] Nums=new int[5]{10,20,30,40,50};

3. Array traversal--advanced for loop

Way One:

Int[] nums={10,20,30,40,50};

Iterates over an array, the array name. Length Gets the lengths of the array

for (int i=0;i<nums.length;i++) {

System.out.println (Nums[i]);

}

Way two:

This introduces a new loop, advanced for Loop (For-each Loop)

Advanced for loop is a new feature after JDK1.5

Grammar:

For (element data type variable name: Array or collection) {

Loop body

}

Example: int[] nums={10,20,30,40,50};

for (int n:nums) {

SYSTEM.OUT.PRINTLN (n);

}

Execution process: equivalent to the array subscript in the element out, assigned to int n, that is, int n=nums[i];

Note: The data types of the elements and the data types of the arrays must be consistent

Usage Scenario: the advanced for loop function is less than a for loop, it cannot manipulate the subscript directly, it is used primarily to iterate over an array

4.

Case 1

1 ImportJava.util.Scanner;2  Public classTestarray {3     /*4 dynamically create arrays based on the number of users entered, and get user input scores, highest score, lowest score and average score5      */6      Public Static voidMain (string[] args) {7Scanner in=NewScanner (system.in);8System.out.println ("Please enter the number of people");9         intnum=in.nextint ();Ten         Double[] scores=New Double[num]; One          for(inti=0;i<scores.length;i++){ ASystem.out.println ("Please enter" + (i+1) + "Student's score"); -scores[i]=in.nextdouble (); -         } the         intSum=0,avg=0; -         DoubleMax=scores[0]; -         DoubleMin=scores[0]; -          for(inti=0;i<scores.length;i++){ +sum+=Scores[i]; -             if(scores[i]>max) { +max=Scores[i]; A             } at             if(scores[i]<min) { -min=Scores[i]; -             } -         } -avg=sum/scores.length; -System.out.println ("Max:" +max+ "\ t min:" +min+ "T-mean score:" +avg); in     } -  to}

Case 2

1 ImportJava.util.Scanner;2  Public classTestarray {3      Public Static intSearchint[] arr,intnum) {4         //queries the specified element in the array arr, or returns 1 if the element exists to return its subscript5         intIndex=-1;6          for(inti=0;i<arr.length;i++){7             if(arr[i]==num) {8index=i;9                  Break;Ten             } One         } A         returnindex; -     } -      the      Public Static voidMain (string[] args) { -         int[] arr={12,34,23,56,3,65}; -         intIndex=search (arr,3); - System.out.println (index); +     } -}

An array of Java foundations

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.