Java Learning (8), static method, Arrays class, two-dimensional array, and arrays two-dimensional array

Source: Internet
Author: User
Tags rounds

Java Learning (8), static method, Arrays class, two-dimensional array, and arrays two-dimensional array

I. Static Methods

Static Methods belong to the class and can be called directly by class name. Method Name.

Static Method Declaration

Access modifier static type method name (parameter list)

{

// Method body

}

The role of the method: a program is divided into several methods, which is conducive to rapid debugging of the program, but also to improve the utilization of the program code. Because the method can be called multiple times, there is no limit on the number of calls and the number of calls.

Method classification: ① return value is (null) void method ② method with specific return type ③ method without parameters ④ method with Parameters

Return Value of a method: if a return value exists in a method, the return keyword must be used in the method to return the value. The return value type is the return value type defined by the method.

① Method without return values

1 public class BubbleSort {2 public static void main (String [] argas) 3 {4 int [] array = }; 5 bubble (array); 6 print (array); 7} 8 9 // bubble Method 10 public static void bubble (int [] array) 11 {12 // N number of rounds compared to N-1 13 for (int I = 0; I <array. length-1; I ++) 14 {15 // the number of times each round of comparison is N-1-i times 16 for (int j = 0; j <array. length-i-1; j ++) 17 {18 // compare adjacent 2 numbers, small front 19 if (array [j]> array [j + 1]) 20 {21 // exchange two numbers. Set the Temporary Variable 22 int temp = array [j]; 23 array [j] = array [j + 1]; 24 array [j + 1] = temp; 25} 26} 27} 28 29} 30 31 // print the output method 32 public static void print (int [] array) 33 {34 // output 35 for (int I = 0; I <array. length; I ++) 36 {37 System. out. print (array [I] + ","); 38} 39} 40}View Code

② Method with return value

1 public class BubbleSort {2 public static void main (String [] argas) 3 {4 int [] array = }; 5 print (bubble (array); 6} 7 8 // bubble Method 9 public static int [] bubble (int [] array) 10 {11 // N number of rounds compared to the N-1 times 12 for (int I = 0; I <array. length-1; I ++) 13 {14 // The number of comparisons for each round is the N-1-i count 15 for (int j = 0; j <array. length-i-1; j ++) 16 {17 // compare adjacent 2 numbers, small front 18 if (array [j]> array [j + 1]) 19 {20 // exchange two numbers, by setting the Temporary Variable 21 int temp = array [j]; 22 array [j] = array [j + 1]; 23 array [j + 1] = temp; 24} 25} 26} 27 return array; 28} 29 30 // print the output method 31 public static void print (int [] array) 32 {33 // output 34 for (int I = 0; I <array. length; I ++) 35 {36 System. out. print (array [I] + ","); 37} 38} 39}View Code

③ Method reuse and without Parameters

1 public class BubbleSort {2 public static void main (String [] argas) 3 {4 int [] array = }; 5 show1 (); 6 print (array); 7 show2 (); 8 print (bubble (array); 9 10 int [] array1 = {80, 25, 12, 30, 35, 22, 55,45, 82,33}; 11 show1 (); 12 print (array1); 13 show2 (); 14 print (bubble (array1); 15} 16 17 public static void show1 () 18 {19 System. out. print ("Before sorting:"); 20} 21 22 public static void show2 () 23 {24 System. out. print ("sorted:"); 25} 26 27 // bubble Method 28 public static int [] bubble (int [] array) 29 {30 // N number of rounds compared to N-1 31 for (int I = 0; I <array. length-1; I ++) 32 {33 // the number of times each round of comparison is N-1-i 34 for (int j = 0; j <array. length-i-1; j ++) 35 {36 // compare adjacent 2 numbers, small front 37 if (array [j]> array [j + 1]) 38 {39 // exchange two numbers. Set the Temporary Variable 40 int temp = array [j]; 41 array [j] = array [j + 1]; 42 array [j + 1] = temp; 43} 44} 45} 46 return array; 47} 48 49 // print the output method 50 public static void print (int [] array) 51 {52 // output the sorted array 53 for (int I = 0; I <array. length; I ++) 54 {55 System. out. print (array [I] + ","); 56} 57 System. out. println (); 58} 59}View Code

Ii. Arrays class

Java jdk provides an Arrays tool class, which provides many proprietary methods for programmers to operate Arrays. By calling methods, you can assign values, sort, and compare Arrays, search for elements.

Search for arrays in the jdk api to view the usage of this class.

For example

1 import java. util. arrays; 2 public class ArraysUtilDemo {3 public static void main (String [] argas) 4 {5 int [] arraySrc1 = {6, 8, 9, 16, 35, 90 }; 6 // copy the array 7 int [] arrayDes1 = Arrays. copyOf (arraySrc1, 10); 8 for (int I = 0; I <arrayDes1.length; I ++) 9 {10 System. out. print (arrayDes1 [I] + ""); 11} 12 13 System. out. println ("\ n **************************"); 14 // copy the data within the specified range in the specified array. 15 int [] arrayDes2 = Arrays. copyOfRange (arraySrc1, 2, 4); 16 for (int I = 0; I <arrayDes2.length; I ++) 17 {18 System. out. print (arrayDes2 [I] + ""); 19} 20 21 System. out. println ("\ n **************************"); 22 int [] arraySrc2 = {8, 6, 10, 16, 35, 90}; 23 boolean flag = Arrays. equals (arraySrc1, arraySrc2); 24 System. out. print (flag); 25 26 System. out. println ("\ n **************************"); 27 // array fill 28 int [] arrayDes3 = new int [10]; 29 Arrays. fill (arrayDes3, 10); 30 for (int I = 0; I <arrayDes3.length; I ++) 31 {32 System. out. print (arrayDes3 [I] + ""); 33} 34 35 System. out. println ("\ n **************************"); 36 // sort the array 37 Arrays. sort (arraySrc1); 38 for (int I = 0; I <arraySrc1.length; I ++) 39 {40 System. out. print (arraySrc1 [I] + ""); 41} 42 43 System. out. println ("\ n **************************"); 44 // search for 45 int x = Arrays in the binary method. binarySearch (arraySrc1, 9); 46 System. out. print (x); 47 48 System. out. println ("\ n **************************"); 49 // use the System class method to copy the array 50 int [] arrayDes4 = new int [10]; 51 System. arraycopy (arraySrc1, 0, arrayDes4, 2, 5); 52 for (int I = 0; I <arrayDes4.length; I ++) 53 {54 System. out. print (arrayDes4 [I] + ""); 55} 56} 57}View Code

3. Two-dimensional array

① It can be viewed as an array with arrays as elements

② The declaration and initialization of two-dimensional arrays in Java should be arranged in the order from high-dimensional to low-dimensional

Example

Int [] [] arr1 = new int [10] []; // The second-dimensional length is not fixed

Int [] [] arr2 = new int [10] [20]; // determine the second-dimensional length.

☆Although there are differences between the two arrays, the size of heap memory allocated by the system is the same.

For any two-dimensional array, the size of the first dimension determines the size of the two-dimensional array object, because the members of the two-dimensional array are array references, and the size of the array references is fixed.

Initialize a two-dimensional array

① Static initialization: int [] [] arr = {1, 2}, {3, 4, 5}, {6, 7, 8, 9 }};

② Dynamic initialization:

String [] [] arrStr; // Declaration

ArrStr = new String [3] []; // creates and allocates memory.

ArrStr [0] = new String [2]; // high-dimensional Initialization

ArrStr [1] = new String [3];

ArrStr [2] = new String [4];

ArrStr [0] [0] = new String ("abc00"); // low-dimensional Initialization

ArrStr [0] [1] = new String ("abc01 ");

ArrStr [1] [0] = new String ("abc10 ");

ArrStr [1] [1] = new String ("abc11 ");

ArrStr [1] [2] = new String ("abc12 ");

ArrStr [2] [0] = new String ("abc20 ");

ArrStr [2] [1] = new String ("abc21 ");

ArrStr [2] [2] = new String ("abc22 ");

ArrStr [2] [3] = new String ("abc23 ");

1 public class ArrayDemo2 {2 public static void main (String [] args) {3 int [] [] arr = new int [3] []; 4 // each high-dimensional array points to a low-dimensional int array 5 arr [0] = new int [2]; 6 arr [1] = new int [3]; 7 arr [2] = new int [4]; 8 9 // assign 10 arr [0] [0] = 1 to a low-dimensional array; 11 arr [0] [1] = 2; 12 arr [1] [0] = 3; 13 arr [1] [1] = 4; 14 arr [1] [2] = 5; 15 arr [2] [0] = 6; 16 arr [2] [1] = 7; 17 arr [2] [2] = 8; 18 arr [2] [3] = 9; 19 20 for (int I = 0; I <arr. length; I ++) 21 {22 for (int j = 0; j <arr [I]. length; j ++) 23 {24 System. out. print (arr [I] [j]); 25} 26 System. out. println (); 27} 28} 29}View Code

 

Related Article

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.