Fourth day of Java learning

Source: Internet
Author: User
Tags array sort

1. Array (static initialization-FAQ)


public class test1{
public static void Main (string[] args) {
Int[] Arr=new int[3];

Arr=null;
System.out.print (arr[1]);
}

At this point the program will error: NullPointerException: null pointer exception, the ARR pointer has not only pointed to the array.


ArrayIndexOutOfBoundsException the array, it accesses a non-existent corner mark.

NullPointerException NULL pointer exception: When the reference does not have any pointer to a value of NULL, the reference also exists in the action entity.


2. Traversal of the array (gets the elements in the array):

public class test1{
public static void Main (string[] args) {
Int[] arr={3,1,2,5,4,6};
for (int i=0;i<arr.length;i++) {
System.out.println ("arr[" +i+ "]=" +arr[i]);
}
}

}

The result of the output is:

Arr[0]=3
Arr[1]=1
arr[2]=2
Arr[3]=5
Arr[4]=4
Arr[5]=6

========================================================================================

3. Define a function output array element and separate it with commas:

public class test1{
public static void Main (string[] args) {
Int[] arr={1,2,3,4,5,6,7,8,9};
PrintArray (arr);
}
public static void PrintArray (int[] arr) {
for (int x=0;x<arr.length;x++) {
if (x!=arr.length-1)
System.out.print (arr[x]+ ",");
Else
System.out.print (Arr[x]);
}
}
}

Output Result: 1,2,3,4,5,6,7,8,9


===========================================================================================

4. Get the maximum value in the array: The idea is as follows

650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M00/89/86/wKiom1gWtS7B_0JHAACfRQd8Fj4851.jpg-wh_500x0-wm_3 -wmp_4-s_60807803.jpg "title=" max.jpg "alt=" Wkiom1gwts7b_0jhaacfrqd8fj4851.jpg-wh_50 "/>

The code is as follows:

public class test1{
public static int Getmax (int[] arr) {
int max=arr[0];
for (int x=1;x<arr.length;x++) {
if (Arr[x]>max)
MAX=ARR[X];

}
return Max;
}

public static void Main (string[] arg) {
Int[] arr={12,2,3,6,5,8,9,88,99,100};
int Max=getmax (arr);
System.out.print ("max=" +max);
}
}

The output result is 100:

Method Two (max initialized to 0) code is as follows--exists as an array of corner marks:

public class test1{
public static int Getmax (int[] arr) {
int max=0;
for (int x=1;x<arr.length;x++) {
if (Arr[x]>arr[max])
Max=x;

}
return Arr[max];
}

public static void Main (string[] arg) {
Int[] arr={12,2,3,6,5,8,9,88,99,100};
int Max=getmax (arr);
System.out.print ("max=" +max);
}
}

===========================================================================================

5. Sort the array (select sort and bubble sort):

The code is as follows:

public class test1{
public static void Selectsort (int[] arr) {
for (int x=0;x<arr.length-1;x++) {///Last number do not compare
for (int y=x+1;y<arr.length;y++) {
if (Arr[x]>arr[y]) {
int temp=arr[x];
Arr[x]=arr[y];
Arr[y]=temp;
}
}
}

}
public static void Main (string[] args) {
Int[] arr={5,6,4,3,2,1,9,8,7};
PrintArray (arr);
Selectsort (arr);
PrintArray (arr);
}
public static void PrintArray (int[] arr) {
System.out.print ("[");
for (int x=0;x<arr.length;x++) {
if (x!=arr.length-1)
System.out.print (arr[x]+ ",");
Else
System.out.print (arr[x]+ "]");
}
}
}

The output results are as follows:

[5, 6, 4, 3, 2, 1, 9, 8, 7] [1, 2, 3, 4, 5, 6, 7, 8, 9]


Bubble sort (two adjacent elements are compared if they match):

The code is as follows:

public  static void Maopao (int[] arr) {
        for (int x=0;x<arr.length-1 ; x + +) {
            for (int y=0;y<arr.length-x-1;y++)
             {
                 if (arr[y]<arr[y+1]) {
                     int Temp=arr[y];
                    arr[y]=arr[y+1];
                    arr[y+1]=temp;
                }
                }
    }

=============================================================================

A common feature of combining the above selection sort and bubbling sort is to replace the values of the two array locations that meet the criteria:

public static void Swap (int[] Arr,int A,int b) {

int Temp=arr[a];

ARR[A]=ARR[B];

Arr[b]=temp;

}

Just call this function and it's OK:

# # #Arrays. Sort (arr); A sort order that has been defined in Java requires the use of this code in the development of an array sort. # # # # # #

===========================================================================================

6, array lookup (binary lookup):

Find the ordinal of the sequence in which the elements in an array are located, with the following code:

public class test1{
public static int GetIndex (int[] arr,int key) {
for (int x=0;x<arr.length;x++) {
if (Arr[x]==key)
return x;
}
return-1;
}
public static void Main (string[] args) {
Int[] arr={2,5,6,9,8,7,9};
int Index=getindex (arr,5);
System.out.print ("index=" +index);
}
}
The above is in order to improve the efficiency of the direct use of binary to find, the code is as follows:

public class test1{
public static int GetIndex (int[] arr,int key) {
for (int x=0;x<arr.length;x++) {
if (Arr[x]==key)
return x;
}
return-1;
}
public static int Halfsearch (int[] arr,int key) {
int Min,mid,max;
min=0;
Max=arr.length-1;
Mid= (max+min)/2;
while (Arr[mid]!=key) {
if (Key>arr[mid])
min=mid+1;
else if (Key<arr[mid])
Max=mid-1;
if (Min>max)
return-1;
Mid= (max+min)/2;
}
return mid;
}
public static void Main (string[] args) {
Int[] arr={2,5,6,7,8,9};
int Index=halfsearch (arr,7);
System.out.print ("index=" +index);
}
}

Fourth 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.