Java from small white to getting started, Day4 (array)

Source: Internet
Author: User

An array is a collection of elements stored in a contiguous block of memory that is used to store multiple data of the same type.

Index (subscript): The number of each data in the array, the Int class, starting from 0 to the length-1 number.

Array item (Element): One of the data in an exponential group. The data type of each data in the array must be the same. An index corresponds to an array item, which corresponds to one by one.

Length: The number of items in an exponential group, in the type int, expressed in length, is fixed.

Array type default value: Number type, 0;           Boolean,false;               Char, ' n '; String,null.

Strings to char: String variable. ToCharArray ()//Expression returned as: char[].

The most basic operation of an array: Save, fetch data.

Core idea: It is the operation of subscript.

Declaring an array: code structure, int [] array=new int[length];

Gets the array item, array[subscript]//subscript as an int class from 0 to Length-1, any number between.

Gets the length of the array, Array.Length//array, for the length of the.

Array initializer: code structure, int [] array={1,2,3,4};//Priority selection.

or int [] array=new int[]{1,2,3,4};

The traversal of the array: In order to get an array of each item, there are For,foreach two ways.

example, int [] array={1,2,3};

For method: for (int i=0; i<array.length;i++) {System.out.print (array[i]+ "\ T"),}//I means subscript, "\ T" is a delimiter.

The Foreach Method: for (int j:array) {System.out.print (array[j]+ "\ T"),}//indicates that each item in the array is fetched sequentially, and then the array item is assigned to the variable J, knowing that the array of indexed array items is exhausted. Efficiency is higher than for.

System.arraycopy (array 1, 0, array 2, 1, array 1.length); Move array 1 from subscript 0 to array 2, subscript to array of 1;

array1=java.util.arrays.copyof;//1: Refers to the variable of the original array, 2: Refers to the length of the new Array (array1). Effects such as: [1,2]→[1,2,0,0].

Quick ordering available in JDK: Java.util.Arrays.sort (array variable);

Bubbling arrangement: rule (forward from back)

? 1. Compare adjacent elements. If the first one is bigger than the second one, swap them both.

2. Do the same work for each pair of adjacent elements, starting from the first pair to the end of the last pair. At this point, the last element should be the maximum number.

3. Repeat the above steps for all elements except the last one.

4. Repeat the above steps each time for fewer elements until there are no pairs of numbers to compare.

Code structure:

for (int i=1;i<=array.length-1;i++) {//I: Control comparison round, 1≤i≤array.length-1, total number of array.length.

for (int j=0;j<array.length-i;j++) {//J: Control subscript, J vs. J+1, 0≤j<array.length-i.
if (Array[i]<array[j]) {
int tmp;
Tmp=array[i];
ARRAY[I]=ARRAY[J];
array[j]=tmp;

Two-dimensional array: int[][] nums=new INT[2][3];//2: Refers to the line. 3: Refers to the column.

Initializer: int[][] Nums = {{34,11,3},{1,2,3}};

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

for (int j = 0; J < Nums[0].length; J + +) {
System.out.print (nums[i][j]+ "\ t");}
System.out.println ();}

Java from small white to getting started, Day4 (array)

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.