java array advanced

Keywords: Java Programming encoding

  1. (fill in the blank) the length of the array is fixed. If you need to expand, you must create a new array. The length of the original array should be copied to the new array.
  2. (fill in the blank) in java, when an array type variable passes a value, it actually passes the address of the array.
  3. (short answer) there are several ways to expand an array. What are the different ways?

Create a new array and move the contents of the original array to the new array.

  1. Use a loop to change the original array address to a new large array address
  2. Use a function to achieve the same expansion effect
  3. The system defined function system.arraycopy is used to realize the capacity expansion;
  4. The system defines the function copyof to realize the capacity expansion;
  1. (fill in the blank) the two-dimensional array implemented in Java is actually the nesting of one-dimensional array.

2-D array also supports display initialization

Two dimensional array can not specify low dimension, and high dimension must be specified; \

  1. (programming) complete the bubble sorting algorithm of the array. Given the array: int[] a = {1,2,3,4,5}, use the bubble sorting to sort it in the order of large to small, and then output the result.
Bubble sorting: compared with two adjacent numbers, cyclic bubble n-1 Second;

class paixu{

         //Bubble sort

         public static void main(String[] args) {

                  int[] a = {4,5,2,1,6,83,9,7,0};

                  for(int i=1;i<a.length;i++){//Note: the initial value of i here should start from 1;

                          //Inner loop control sorting times

                          for (int j=0;j<(a.length-i);j++) {

                                   //Exchange order, compare the size of two numbers, large backward

                                   if (a[j]>a[j+1]) {

                                            int t;

                                            t=a[j];

                                            a[j]=a[j+1];

                                            a[j+1]=t;

                                   }

                          }

                  }

                  //Loop again to see if the sorting is successful

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

                          System.out.print(a[i]+"\t");

                  }

         }

}
  1. (programming) complete the selection sorting algorithm of the array in the above question.
Select sort: ratio of one number to all remaining numbers

class xuanze{

         //Select sort to compare a number to all remaining numbers

         public static void main(String[] args) {

                  int[] a = {4,5,2,1,6,83,9,7,0};

                  //Outer loop starts from 0, loop length-1 times

                  for (int i=0;i<a.length-1 ;i++ ) {

                          //From i+1 to length,

                          for (int j=i+1;j<a.length ;j++ ) {

                                   //Compare the first number with all the following numbers in turn. Put the smallest number first and change the order

                                   if (a[i]>a[j]) {

                                            int t=a[i];

                                            a[i]=a[j];

                                            a[j]=t;

                                   }

                          }

                  }

                  //Loop through the elements of the array again

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

                          System.out.print(a[i]+"\t");

                  }

         }

}
  1. (programming) complete the fast sorting algorithm of the array in the 4 questions.
class kuaisu{

       //Quick sort

       public static void main(String[] args) {

              int[] a = {4,5,2,1,6,83,9,7,0};

              //Sort uses the quick sort method,

              java.util.Arrays.sort(a);

              //Loop through the elements of the array again

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

                     System.out.print(a[i]+"\t");

              }

       }

}

Improve exercises:

1. (encoding) output a given array int[] a={1,2,3,4,5,6} in reverse order.         

 

 class nixu{

         //Output a given array int[] a={1,2,3,4,5,6} in reverse order.

         public static void main(String[] args) {

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

                  for (int i=a.length-1;i>=0 ;i-- ) {

                          System.out.print(a[i]+"\t");

                  }

         }

}

 

Posted by eagleweb on Mon, 02 Dec 2019 21:25:22 -0800