Java Fundamentals (Chapter eight)

Source: Internet
Author: User

Java Fundamentals (Chapter eight)

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

L Declaration and definition of syntax:

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

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

• Allocating space for an array

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

L declare 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

1. get the value of a single element

Data type variable name = array name [subscript];

2. iterate through 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];

}

Iv. How to assign values to the elements 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};

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 is 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 to the imaginary maximum (small) value corresponding to the variable "assign value to the imaginary value variable"

Seven, 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

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

Nine, (a) is known an array, how to insert a "new element" into the array at the specified position?

Implementation ideas:

1, define a length of "known quantity leader degree +1" of the new array yy

2. Using the 1th loop, save the value of each element in the known quantity group to the new array yy

3, using the 2nd loop, the element in the new array yy, starting from the bottom 2 ( the length of the new array yy -2) element , to the specified position , move backward one position

4. Assign the "new element" directly to the specified element in the new array yy (subscript to the specified position)

5. Use the 3rd loop to output the value of the new array yy

(ii) a known array in ascending or descending order, inserting a new value into the array, and after the insertion succeeds, the array is still sorted in ascending or descending order?

1, set the insertion position variable index's initial value is: array length-1;

2. Use the 1th loop to make a comparison between the inserted value and each element in the array, and if the inserted value is larger or smaller than the value of an element in the arrays, assigns the underlying value of the element to the insertion position variable index and terminates the loop

3. Use 2nd loop, move position, from "Array length-2" to "Insert position variable index"

4. Save the inserted value to the position labeled index below the array

5, use the 3rd loop, output the value of all elements of the array, and output the position where the new value is inserted

Java Fundamentals (Chapter eight)

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.