Java Basics (5)

Source: Internet
Author: User

What is an array and how does it work?

Definition: A collection with the same data type

Function: Stores contiguous data of the same type

Ii. how to declare and define arrays in Java

2.1 Syntax for declaration and definition:

data type [] array name; (int[] nums;)  or data type array name []; (int nums[];)

2.2 Allocating space for an array

Array name =new data type [length of array]; (Nums=new int[5];)

2.3 Declaring an array, allocating space at the same time

data type [] Array name = new data type [length of array];

Data type array name []=new data type [length of array];

(int[] nums=new int[5];) (int nums[]=new int[5];)

Third, how to read the elements in the array- by subscript ( index ) to get

the Subscript (index) is from 0 first, the maximum subscript is Array length -1

Subscript out of bounds: ArrayIndexOutOfBoundsException

int array of type, the default value for each element is 0

String array of type, the default value for each element is NULL

3.1 get the value of a single element

Data type variable name = array name [subscript];

3.2 Loop reads each element in the array

The length of the array: array name . Length Property automatic calculation

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

Array name [i];

}

Four. How to assign a value to an element in an array

Mode 1 (give array fixed value):

Declares an array, allocates space, and assigns values

( The length of the array is determined by the number of values )

Data type array name []=new data type []{value 1, value 2 ... Value n};

Or

data type [] Array name ={value 1, value 2 ... Value n};

Description: [] brackets can be placed before or after the array name

Mode 2: Dynamically assign values to the array (input from the keyboard)

Scanner input = new Scanner (system.in);

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

Array name of string type [i]= input.next ();

Array name of type int [i]= input.nextint ();

An array of type Double [i]=input.nextdouble ();

}

Five, array sorting problem

Use arrays. Sort (array name);

Implementing an element in an array output in ascending or descending order

Six, to find the maximum or minimum value

Implementation idea: The value of the 1th element in the array, assumed to be the maximum (small) value "assignment", and then each element in the array is compared with the imaginary maximum (small) number, if the value of an element in the array is larger (small) than the maximum (small) number, the value of the element is saved in the variable corresponding to the imaginary maximum (small) value

 Public classShuzu { Public voidDint[]num] {        intha []=New int[10];  for(intj=0;j){            intIndex= (int) (Math.random () *100); HA[J]=index; System.out.print (Ha[j]+" "); }        intMax=ha[0]; intMin=ha[0];  for(inti=1;i){            if(max<Ha[i]) {Max=Ha[i]; }            if(min>Ha[i]) {min=Ha[i]; }} System.out.println ("\ n" + "Maximum value:" +max); System.out.println ("Minimum value:" +min); }}

Eight, to find the most value in the array position

1, outside the loop, define the variable K (assigned to 0), to save the most value of the subscript

2, in the cycle, the comparison to find the maximum value of the same time, the variable K assignment

     Public Static  voidmain (String [] args) {Scanner input=NewScanner (system.in); Char[] ha={' a ', ' C ', ' U ', ' B ', ' e ', ' P ', ' f ', ' Z '}; System.out.print ("Original character sequence:");  for(inti=0;i) {System.out.print (Ha[i]+" "); } arrays.sort (ha);//descending an arraySystem.out.print ("\ n in ascending order:");  for(inti=0;i) {System.out.print (Ha[i]+" "); }         Charhe[]=New Char[9];//create a new array to store the data          for(inti=0;i) {He[i]=Ha[i]; } System.out.println ("\ n the character inserted is:");//Enter new data         CharA=input.next (). CharAt (0); intIndex=he.length-1;//where to save new data insertions          for(inti=0;i){             if(A//to judge a numberIndex=i;//find the subscript position of the new element                  Break; }         }          for(ints=he.length-2;s>=index;s--) {He[s+1]=he[s];//move an element from behind} He[index]=a;//assigns the input value to the arraySystem.out.println ("\ n inserted subscript is:" +index);  for(intk=0;k) {System.out.print (He[k]+" "); }    }

Viii. Fibonacci Sequence

1,1,2,3,5,8,13,21,34 ... Find the value of the 20th number? (using loops)

Rule: First i + (i+1) = (i+2)

Step 1: Define an array with an array length of 20

int []nums=new int[20];

Step 2: Get the initial value of the No. 0 and 1th numbers according to the law

Nums[0]=1;

Nums[1]=1;

Step 3: Assign values to all elements in the array by looping

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

Nums [i+2]=nums[i]+nums[i+1];

}

Or

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

NUMS[I]=NUMS[I-2]+NUMS[I-1];

}

Step 4: Output NUMS[19] Value

Java Basics (5)

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.