Thinking in Java Learn (8)

xiaoxiao2021-03-06  66

Array's initialization Array is actually a set of objects or a set of basic data types of data, and the data in each Array must be the same type, and in one indicator is packaged, for example, int [] i; or int i []; Now you declare an array, but the Array is actually a set of handles, not specific data objects, you must write the initialization statement to make Array make meaning, initialization, and 2 inti [] = New INT [3]; or INT i [] = {1, 2, 3}; it is necessary to note that the second initialization method can only occur when Array is generated. Java allows you to assign an Array to another Array, and if you pass, it is just a handle, let's see an example.

Public class arrays {public static void main (string args []) {int [] i = {1, 2, 3}; // declare an array, and initialize int [] II; // only declare an array II = i The // // handle does not pass the value within the array, in other words, the handle of II and I is always pointing to the same memory address for (int J = 0; j

Class Tank {static int counter = 0; int id = counter ; boolean full = false; public tank () {system.out.println ("tank" ID "created"); fullic void Empty ) {Full = false;} protected void finalize () {if (full) system.out.println ("Error: Tank" ID "Must Be Empty At Cleanup"); Else System.Out.println ("Tank" ID "cleaned up ok");}}} class tanktest {public static void main (string args []) {new tank (); new tank (). EMPTY (); system.gc ();}} 16, Write a class, including 2 String data, one initialized in the constructor, one initialized at the definition, what is the difference between this 2 China France

Class test {string S1 = "Hello!"; String S2; public test (string s) {s2 = s;} public static void main (string args []) {test t = new test ("Goodbye"); system. Out.println (T.S1); // static function can only call Static's data and functions, but the object. In addition to system.out.println (t.s2);}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}} The function call is completed before, while S2 is also initialized, but he is initialized into NULL, and then S2 can vary depending on the value of the value transmitted to the constructor, more flexibility. 18. Write a class, there is a String data, use the entity to initialize the way, and speak different purposes with this book.

Class test {string s; {s = "hello!"; // Notice not to write String S = "Hello", otherwise it will become a local variable! System.out.println ("Instance Before Constructor");} Test () {system.out.println (S "from test ()");} public static void main (string args []) {test t = new test ();}} The use of the book is that this usage must be used in anonymous internal classes (now there is still not learned), because constructor needs to have a function name, but anonymous internal classes are no name So we need to use this method to initialize the data! Let's use it again: In the general class, the physical initialization method is called before the constructor call, then in this case, this program will first display the Instance Before Constructor and then display Hello! From test (), enough to explain the problem. 20. Write a function, can generate a three-dimensional array and initialize, the capacity of the array is determined by the number of functions, and the initial value of Array must be within the range specified by the function factor, and then write a function. An array of different capacities is required in the main function

Class Test {// Accept the quotient function, x, y, z represent the elements in the array, D1, D2 represent the range of array, and the function can return a array public static double [] [] CreateArray (INT X) , int y, int z, double d1, double d2) {double [] [] [] DARRAY = new double [x] [y] [z]; // Define an array, not initialization Double Incr = (D1-D2 ) / (x * y * z); // calculate the increment of each array value, because the value of our array is in the range of D1-D2 Double Value = D2; // A value of each element in the array, and Let him initial value equal to the minimum value of the transfer quotes for (INT i = 0; i

转载请注明原文地址:https://www.9cbs.com/read-120209.html

New Post(0)