Home > JAVA > Array

Array

Declaring an array:

int[] key;
Thread[] threads;
String[][] occupantName;

Initialized array:

int[] key = new int[5];
Thread[] threads = new Threads[4];
String[][] occupantName = new String[10][]; //first bracket must given an size, second is optional.
int[] carList = new int[]; //will not compile, need size
String[] days = {"Sunday", Monday", Tuesday", "Wendsday", "Thirdday", "Friday", "Saturday"}; //the size determined by the number of comma-separated items between the curly braces

Note:

  1. Arrays can hold primitives or objects, but the array itself is always an object.
  2. When you declare an array, the brackets can be left or right of the name.
  3. It is never legal to include the size of an array in the declaration.
  4. You must include the size of an array when you construct it (using new) unless you are creating an anonymous array.
  5. Elements in an array of object are not automatically created, although primitive array elements are given default value.
  6. You’ll get a NullPointerException if you try to use an array element in an object array, if that element does not refer to a real object.
  7. Arrays are indexed beginning with zero.
  8. An ArrayIndexOutOfBoundException occurs if you use a bad index value.
  9. Arrays have a length variable whose value is the number of array elements.
  10. The last index you can access is always one less than the length of the array.
  11. Multidimensional arrays are just arrays of arrays.
  12. The dimension in a multidimentional array can have differeny lengths.
  13. An array of primintives can accept any value than can be promoted implicitly to the array’s declared type. Example byte variable can go in an int array.
  14. An array of object can hold any object that passes the IS-A (or instanceof) test for the declared type of the array.
  15. If you assign an array to the previously declared array references, the array you’re assigning must be the same dimension as the reference you’re assigning to.
  16. You can assign an array of one type to a previously declared array reference of one of is supertypes.

 

 

Categories: JAVA
  1. No comments yet.
  1. July 1, 2014 at 2:01 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: