Array of Java languages-----Select sort

Source: Internet
Author: User

1. Basics of Arrays: arrays are the collections used to store variables

2. Declaration format of an array: element type [] Array reference variable name = new element type [number of elements]

For example: int [] arrary = new int [10] declares and creates a 10 element with type int arrary is a reference variable with an array of 10 elements of type int

3. Initialization of arrays:

int [] arrary = new int [] {1,2,3,4,56,7};

or int [] arrary = new int [6];

arrary [0] = 1;............arrary [5] = 7;

or int [] arrary = {1,2,3,4,56,7};

3. For Each loop: The entire array can be traversed sequentially without using subscripts

for (double u:mylist) {

SYSTEM.OUT.PRINTLN (U);

}//For each element in MyList, do the following

4. Three ways to copy an array:

1) Copy array elements one by one using loop statements

such as: int Arrary1 [] = {1,2,3,4,5,6};

int Arrary2 [] = new int [arrary1.length];

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

Arrary2 [i] = arrary1 [i];

}

2) Use the static method in the System class Arrarycopy

3) Copy the array using the Clone method

5. Passing arrays to methods

public static void Printarrary (int [] arrary) {

for (int i = 0; i < arrary.length) {

System.out.println (arrary [i]);

}

}

public static void Main (String [] args) {

int [] Getarrary = {1,2,3,4,5};

Printarrary (getarrary);

}

6. Select the Sorting method

Find the smallest number in the array first, and then put it in the first digit of the sequence. Next, find the minimum number in the remaining number and put it behind the first number, and so on until only one number is left.

Such as:

Class exerc05{

public static void Selectsort (int []arrary1) {
for (int i = 0; i<arrary1.length-1; i++) {
for (int j = i + 1; j<arrary1.length; J + +) {
if (Arrary1[i] > Arrary1[j]) {
int temp = Arrary1[i];
Arrary1[i] = Arrary1[j];
ARRARY1[J] = temp;
}
}
}
}


public static void Printarrary (int[] arrary3) {
System.out.print ("[");
for (int k = 0; k<arrary3.length; k++) {
if (k!= arrary3.length-1) {
System.out.print (Arrary3[k] + ",");
}
Else
System.out.println (Arrary3[k] + "]");
}
}


}


Class exerc06{
public static void Main (String[]args) {
EXERC05 arr = new EXERC05 ();

Int[] Arrary2 = {1,3,6,7,2,9};

int arr1 = Arr.selectsort (Arrary2); You cannot assign an array to a variable; compile an error;
Print before sorting
Arr.printarrary (Arrary2);
System.out.print (ARR1);
Arr.selectsort (Arrary2);
Print after sorting
Arr.printarrary (Arrary2);
}
}

Array of Java languages-----Select sort

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.