JAVA-array

  Module 5 array … 1 

  1. Array statement … 2 

  2. Array … 2 

  2.1. Create an array of primitive data types … 2 

  2.2. Types of data objects to create an array … 3 

  3. Array initialization … 3 

  4. Multidimensional array … 4 

  5. Array boundaries … 4 

  6. Copy arrays … 5 

  7. Practice: the use of an array … 5 

  7.1. Basic array using … 5 

  7.2. Array array … 6 

  7.3. Check your progress … 6 


  This module will describe how the Java programming language in the definition and use of the array initialization. 

  Completion of this module of learning, you should be able to: 

  — Declaration and the creation of the original array, and an array of multi-dimensional arrays 

  — Explain why to initialize the array element 

  — Is the array definition initialize the array elements and 

  — Identify a number of elements in an array of 

  — Prepared by the code will be an array of copying data from one another array 

  1. Array statement 

  Array is used to represent the same type of data, you can declare any type of array ─ ─ primitive type or category type: 

  Char s []; 

  Point p []; / / where point is a class 

  In the Java programming language, regardless of array elements from the original type of composition, or by category structure, the array is an object.    Statement could not object creation itself, but rather the creation of a quote, the quote could be used to invoke the array.    The actual use of the array elements can be new language or memory array initialization software dynamically allocated. 

  This will be the variable name in square brackets placed in a statement after the array format for C, C + + and Java programming language standard format.    This format will the format of the statement complex difficult, therefore, the Java programming language to allow an alternative format, the format in the square brackets in the left side of the variable name: 

  Char [] s; 

  Point [] p; 

  As a result, you can believe that some type in the left, and the variable name in the right.    Coexistence of the two formats, you can choose your habits in a way.    Declaration is not that the actual size of the array. 

  Note: When the brackets array statement on the left, the square brackets can be used in all the variables in his right. 

  Char [] s1, s2; / / s1, s2 are an array of characters. 

  [] Char s1, s2; / / is an array of characters s1, s2 is the characters. 

  2. Array 

  Used to indicate a single element array subscript must always start from zero, and maintaining the legitimate scope of the - greater than or equal to 0, and less than the length of the array.    Visit any of the above-mentioned limits of the array elements will lead to an attempt to run error.    Below better to turn to some of the array initialization method. 

  2.1. Creation of the original array data type 

  CreateIntArray public int [] () stack heap 

  ( 

  Int [] 

  A 

2

3

4

5

  Int [] k = null; 

  K = new int [5]; 

  For (int I = 0; I <k.length; I + +) 

  K [I] = I +1; 

  Return k; 

  ) 


  CreateIntArray k 

  This 

  Main 

  You can create as the same object, the use of keywords to create a new array. 

  K = new int [5]; 

  Creation of a five int value of the array, 

  2.2. Create Object array data type 

  3. Array initialization 

  When creating an array, each element has been initialized.    Char s array in the above example, each value has been initialized to 0 (\ u0000-null) characters in the array p example, each value has been initialized to null, it also shows that not use a Point object.    After assignment p [0] = new Point (), the first element of the array used for the actual Point object. 

  Java programming language allows the use of the following forms quickly create an array: 

  String names [] = ( 

  "Georgianna" 

  "Jen" 

  "Simon" 

  ); 

  The result equated with the following code: 

  String names []; 

  Names = new String [3]; 

  Names [0] = "Georgianna"; 

  Names [1] = "Jen"; 

  Names [2] = "Simon"; 

  This "shorthand" method can be used in any type of elements.    For example: 

  MyDate Dates [] = ( 

  New MyDate (22,7,1964) 

  New MyDate (1,1,2000), 

  New MyDate (22,12,1964) 

  ); 

  Appropriate type of the regular numerical also can be used: 

  Color palette [] = ( 

Color.blue,

Color.red,

Color.white

  ); 

  4. Multidimensional array 

  Java programming language does not provide as other languages as multi-dimensional arrays.    Because of a statement array can be any basis for a type, so you can create an array of arrays (and the array of the array array, etc.).    A two-dimensional array (JAVA not the concept of two-dimensional array, an array of two-dimensional arrays is actually an array) as in the example below: 

  Int twoDim [] [] = new int [] [4]; 

  TwoDim [0] = new int [5]; 

  TwoDim [1] = new int [5]; 

  Calling the first time created the new object is an array, which includes four elements, each element of the array of int type element is a null and must be used for each element of each array initialization. 

  NOTE: 

  Although the format of the statement to allow variable name in square brackets in the left or the right, but such flexibility does not apply to other aspects of the array syntax. 

  For example: new int [] [4] is illegal. 

  Because of the difference between each element of initialization, it is possible to create non-rectangular array of arrays.    In other words, twoDim elements can be initialized as follows: 

  TwoDim [0] = new int [2] 

  TwoDim [1] = new int [4]; 

  TwoDim [2] = new int [6]; 

  TwoDim [3] = new int [8]; 

  As such initialization methods cumbersome boring, and an array of rectangular array is the most common form, thus producing a "shorthand" method to create a two-dimensional array.    For example: 

  Int twoDim [] [] = new int [4] [5]; 

  Can be used to create an array with five each integer array of four types of array. 

  5. Array boundaries 

  In the Java programming language, all of the array subscript are starting from 0.    A number of elements in an array of attributes stored in length; This value is used to check all run-time visit boundaries.    If there is one beyond the limits of the visit, then the error will run there. 

  Examples of the use of length attribute as follows: 

  Int list [] = new int [10]; 

  For (int i = 0; i <list.length; i + +) ( 

  System.out.println (list [i]); 

  ) 

  Length attribute makes use of the safeguard procedures more simple. 

  6. Copy arrays 

  Once created array, the size of the adjustment.    However, you can use the same variables used to invoke a new array: 

  Int myArray = new int [] [6]; 

  MyArray = new int [10]; 

  In such circumstances, an array of discarded, unless it cited other reservations in other places. 

  Java programming language in the System category provides a copy of an array of special methods, the method is called arraycopy ().    For example, araycopy available for the following: 

  Int myArray [] = (1, 2, 3, 4, 5, 6) / / original array 

  Int hold [] = (10, 9, 8, 7, 6, 5, 4, 3, 2, 1) / / new larger array 

  System.arraycopy (myArray, 0, hold, 0, myArray.length); / / copy all of the myArray array to the hold 

  / / Array, starting with the 0th index 

  At this time, hold an array contains the following contents: 1,2,3,4,5,6,4,3,2,1. 

  Note: If array preservation is a basic type of copy directly to the value of over.    If the storage array is invoked type (class types, an array type (multidimensional array copies), etc.), then copy the address type is used.    Look at this example: 

  Class TestDArray 

  ( 

  Public static void main (String [] args) 

  ( 

  Int [] [] a = new int [] [5]; 

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

  A [i] = new int [i +1]; / / array to the size of each element initialization 

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

  ( 

  For (int j = 0; j <a [i]. Length; j + +) 

  ( 

  A [i] [j] = i + j; 

  ) 

  ) 

  Int [] [] b = new int [a.length] []; 

  System.arraycopy (a, 0, b, 0, a.length); 

  For (int i = 0; i <b.length; i + +) 

  ( 

  For (int j = 0; j <b [i]. Length; j + +) 

  ( 

  B [i] [j] = i + j; 

  System.out.print (b [i] [j]); 

  ) 

  System.out.println (); 

  ) 

  ) 

  ) 

  7. Practice: the use of an array 

  7.1. Use an array of basic 

  1. BasicArray the creation of a category called in the … main () statement in the two variables, one is thisArray, and the other is thatArray, they should be type array of int. 

  2. Create an array, which has 10 int values range from 1 to 10.    This third distribution of the application to the array variables thisArray. 

  3. Use for () Print thisArray cycle all value.    How to control the number of cycle? 

  4. Compile and run a program.    How much value are printed?    What are these values? 

  5. ThisArray each of the elements to establish its value for the Index value of the factorial.    Print values in an array. 

  6. Compile and run a program. 

  7. ThisArray allocated to the application of variable thatArray.    Print thatArray all the elements. 

  8. Compile and run a program.    TyatArray was revealed that the number of values?    What are these values?    Where they come from. 

  9. ThisArray modify certain elements, print thatArray value. 

  10. Compile and run a program in thatArray value, you took note of what? 

  11. Creation of a 20 int value of the array.    The allocation of the new array to the application of variable thatArray, print thatArray value. 

  12. Compile and run a program.    Each array was revealed that the number of these values? 

  13. Copy thisArray value to thatArray.    What you will use Method Invocation? You will be how to limit the number of copies elements? ThatArray elements 10-19 any changes? 

  14. Print thatArray value. 

  15. Compile and run a program.    You show that the value is correct?    If not, you know that there are those who are not content to understand? 

  16. Change thatArray certain value; Print thisArray and thatArray. 

  17. Compile and run a program.    These values are you looking forward to? 

  7.2. Array array 

  1. Array2D the creation of a category called in the main () method in a statement called twoD variables, it should be type array of array of int. 

  2. Create an element of type int array, the array should include four elements and was assigned to the variable twoD elements [0]. 

  3. Two nested for the preparation of () loop to print twoD the full value.    Arranged in a matrix format output (available System.out.print () method). 

  4. Compile and run a program.    At this point you should be able to find a running error (null pointer abnormal), it is because twoD of elements [1] to [3] is not initialized. 

  5. Were created, including five, six and seven elements int array, the array will be used to twoD were the elements [1], [2] and [3]; confirm completion of the operation of the code is in Step 3 described nested for () before insertion cycle. 

  6. Compile and run a program.    This you should see a non-zero value of the rectangular layout. 

  7. TwoD given each element of the array is a clear non-zero value (Hint: Use Math.random () to obtain random value). 

  8. Statement an array of int is a type called oneD variables.    Then, including the creation of a four element array of int.    The application of the array were given an array array twoD oneD and the first element.    After the assignment, print and twoD oneD array. 

  9. Compile and run a program.    Please note that through print oneD shown the value of a single array and twoD array element is the same. 

  7.3. Check your progress 

  In the next module, please confirm you are able to: 

  — Declaration and the creation of the original array, the array or an array of type 

  — Explain why to initialize the array element 

  — Is the array definition initialize the array elements and 

  — Identify a number of elements in an array of 

  — Create multi-dimensional arrays 

  — Prepared from an array type to another type of the array of arrays copy code 

Tags: , , , , , , , , ,

Releated Java Articles

Comments

Leave a Reply




Java array

  When you compile the cases so as not to create static array. 
  Int name [50]; / / compiler will produce an error 

  You do not use the new operator to fill a no definition of the size of the array. 
  Int name []; 
  For (int i = 0; i <9; i + +) 
  (Name [i] = i;) 

  You can only: 
  String names [] = ( "name1," "name2", "name3", "name4"); 
  Int scores 1,2,3,4 [] = (); 

  Or 
  / / Int [] = (a); 
  Int [] a; 
  A = new int [11]; 
  / / A.length = 11; 
  For (int i2 = 0; i2 <= 10; i2 + +) 
  ( 
  A [i2] = i2; 
  System.out.println ( "a [" + i2 +"]="+ a [i2]); 
  ) 

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • DotNetKicks
  • DZone
  • Netvouz
  • Propeller

Tags: , , , , , , , , ,

Releated Java Articles

Comments

Leave a Reply