11th Day of Java Learning

Source: Internet
Author: User

12th Time Class

Goal

One-dimensional array (create access)

I. Concepts and features 1, concept

Ordered collections of the same data type []

Array Name: The name of the container

Element: Subscript variable, array name [subscript]

Lengths: Length

Subscript: Position, index [0,length]

2. Features

1), array is a reference type, is also an object (new), the element is considered a member property

2), array is a container, once the length is determined immutable (new open space)

3), elements of the same data type

A), basic type: int[] double[]

b), reference type: string[] man[] dog[]

4), the position is orderly, not refers to the content is orderly

II. Declaration and initialization 1, using default values

Type [] Array name =new type [length];

Type array name [] =new type [length]; À do not recommend the writing of C language

2. Manually given value

Type [] Array name =new type []{value list};

Omitted form: Type [] Array name ={value list};

The omitted form cannot be separated from the declaration

Three, the traversal of the array

Array access If there is no regular, only one element of access, if there is a rule, you can loop to traverse access.

1. General for

2. Enhanced for foreach

Note: foreach is commonly used to get data because local variables copy the contents of an element.

3, exercise (can make the first 3)

1), store 1-5 integers, accumulate sum

2), Analog Supermarket shopping print small ticket (list goods) checkout

3), store 1-20 in the specified array

4), store the value of the multiplication table, sum

5), 5 people, make a circle, every 2 exit one, print out the order, the last number is who (understand ...)

Represents 5 persons-->false existence true indicates leaving

Boolean [] people =Newboolean[10];

Kicking people constantly.

inti=0;

intlen =people.length;

intj=0; 0 1

while (len>0) {

Continuous 0 1 2 3 4 loop Lookup

if (people[i]==false) {

j + +; Find a

if (j==2) {

People[i]=true;//Leave

len--; One less.

System. out. println (i);

j=0;

}

}

i++;

I=i%people.length;

}

6), randomly generate 100 lowercase letters, statistics print the probability of each letter appearing

A) randomly generated lowercase letters

b) Generate 100,

c) Statistical probabilities

7), using array implementation: There are three people, Zhang San (24 years old), John Doe (28 years old), Harry (22 years old); Write program implementation:

There is an int variable in the program, and when its value is a person's age,

Print out the person's name. And when the value does not equal the age of these three people, the output of no one's age is [value].

Such as:

a) int age=22; the output Harry

b) int age=12; no one's age is 12.

Iv. bounds of the array (avoid runtime Exceptions) 1, NullPointerException

No space to open up

2, ArrayIndexOutOfBoundsException

Array out of bounds, range in [0, length]

3, Negativearraysizeexception

Negative length, compile pass, run wrong

Five, command line parameters (learn)

1. Cmd Pass Parameters

Java ArrayDemo11 a b c "D E F"

2. Eclipse Delivery

Vi. Array Method 1, Equals ()

Does not override the Equals method, compares or addresses

2, Arrays.equals ()

Compare the contents of an array

3, Arrays.sort () –> understand

Sort from small to large (default, ascending) or from large to small (descending)

Know:

1), comparable +compareto

2), Comparator +compare

11th Day of Java Learning

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.