Java Array Chapter

Source: Internet
Author: User

Array is a variable that stores a set of data of the same data type (that is, a data type that can store many numeric values)

If declaring a variable is an appropriate space in the memory space, declaring an array is a contiguous set of spaces in the memory space.

Basic Requirements for arrays

    • Identifier: The name of the array used to distinguish between different arrays
    • Array elements: Data stored in an array
    • Element subscript: Starting from 0
    • Element type: The data type of the array

Basic steps for array manipulation

  1) Declaring an array: Tell the computer what the data type is

data type [] array name; such as: int[] score1;

Data type array name []; such as: int score2[];

 Note: The length of an array cannot be declared when declaring an array

  2) Allocate space: Tell the computer to allocate several contiguous spaces

such as: Score=new int[30];

    Data type []  new  data type [size];

 3) Assign Value

  Edge Declaration Variable assignment value

Int [] score=new int[]{89,79,69}; There are 3 values in front of 3, the computer does not allow

Int[] score={89,69,79}

You can also use console input to assign values

Scanner  input =new  Scanner (system.in); for (int  i=0; i<30; i++) {score[i]=input.nextint ();}

Example:

/** Define an array sequence * The value of the loop output sequence number of all values in the series and * guessing games: Enter any data from the keyboard to determine if the number is included in the sequence*/ Public classTest01 { Public Static voidMain (String args[]) {int[] arr; Arr=New int[]{8,4,2,1,23,34,12}; intSum=0;  for(inti=0;i<arr.length;i++) {System.out.println (arr[i]); Sum=sum+Arr[i]; } System.out.println ("The sum of the array sequence is:" +sum); Scanner input=NewScanner (system.in); System.out.println ("Please enter a number:"); intj=Input.nextint (); intK=0;  for(inti=0;i<arr.length;i++){            if(j==Arr[i]) {k=1;  Break; }        }        if(k==1) {System.out.println (Contains); }Else{System.out.println ("Does not contain"); }    }}

Java Array 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.