Basic knowledge of Java arrays

Source: Internet
Author: User

    • The definition of the array: int[] array=new array[n];int array[]={,,,,}; Defines an array, the JVM gives it a space, the array is the type of data that is applied, and it is stored in a random way.
    • The traversal of an array is typically traversed by looping through its elements, and two-dimensional arrays can be traversed by frost or double loops. The subscript for the array starts at 0.
    • Array substitution, the array provides a method, two overloaded forms, this method is fill (), this method can be used to populate the array or replace the element at the specified position in the array.
public class Array_fill {public static void main (string[] args) {int[] array = new int[5];//defines a one-dimensional array containing five elements Arrays.fill (AR Ray, 8);//Use the static method fill of the arrays class to populate the array elements//elements in the output array for (int i = 0; i < Array.Length; i++) {System.out.print (Array[i] + "") ;} System.out.println ();//Replace element Arrays.fill (array, 2, 5, 5);//output array for (int i = 0; i < Array.Length; i++) {System.out.print (Array[i] + "");}}}
  • Array ordering: The array itself has the sort () method in the Arrays class, where several common sorting methods are written. (1) Bubble sorting method
    public static void Main (string[] args) {Scanner in=new Scanner (system.in), int i,j;//i is used to store the value of the smallest element of an array, j is used to position the element int[] Array=new int[10];//defines an integer array of length 10 int itemp=0;//variable that represents the array element minimum value int ipos=0;//represents the lowest element subscript System.out.println ("Please enter 10 number:"); for (int n=0;n<array.length;n++) {//Traversal 10 elements of the input array Array[n]=in.nextint ();} /* * Sort array */for (i=0;i<9;i++) {//Outer loop itemp=array[i];//Set current element to minimum ipos=i;//record minimum element position for (j=i+1;j<10;j++) {// The inner loop represents the predicted urea to be compared with the smallest element and the number of times if (array[j]<itemp) {//If later elements have a smaller number than the temporary minimum value, then the value of the re-assignment is the minimum itemp=array[j];ipos=j;} The position of the exchange minimum value array[ipos]=array[i];array[i]=itemp;}} Output array System.out.println ("Select order from small to large:"); for (int n=0;n<array.length;n++) {System.out.print (array[n]+ "\ t");}

    (2) Bubble sorting method

    public class Maopaopaixu {public static void main (string[] args) {Scanner in=new Scanner (system.in); int i=0,j=0;//to control the inner and outer layers Loop int[] Array=new int[10];int itemp=0;//is used to store 10 elements of the intermediate variable//input array System.out.println ("Please enter 10 numbers:"); for (int n=0;n< array.length;n++) {array[n]=in.nextint ();} Bubble Sort method for (i=0;i<array.length-1;i++) {for (j=i;j<array.length-1;j++) {if (array[j]>array[j+1]) {iTemp= array[j];array[j]=array[j+1];array[j+1]=itemp;}}} System.out.println ("After bubble sort:"); for (int n=0;n<array.length;n++) {System.out.print (array[n]+ "\ t");}}

    (3) Exchange sorting algorithm

    Package Arrayssort;import java.util.scanner;/** * Exchange sorting algorithm: The interchange sorting algorithm is to compare each digit to all other numbers, and if the data that meets the criteria is found, the Exchange location * @author Administrator * */public class Jiaohuanpaixu {public static void main (string[] args) {Scanner in=new Scanner (system.in); T i=0,j=0;int[] array=new int[10];int itemp=0; System.out.println ("10 numbers of input arrays:"); for (int n=0;n<array.length;n++) {array[n]=in.nextint ();} Interchange sort for (i=0;i<array.length-1;i++) {for (j=i+1;j<array.length;j++) {if (Array[i]<array[j]) {itemp=array[i ];array[i]=array[j];array[j]=itemp;}}} System.out.println ("The number of digits after the interchange is:"); for (int n=0;n<array.length;n++) {System.out.print (array[n]+ "\ t");}}

    (4) Insert Sort method (c basic sort algorithm inside)

Basic knowledge of Java arrays

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.