Difference between revisions of "Arrays"

From Suhrid.net Wiki
Jump to navigationJump to search
Line 83: Line 83:
 
* It also works for the whole array itself. a subtype array can be assigned to a super-type array.
 
* It also works for the whole array itself. a subtype array can be assigned to a super-type array.
 
* Works for interfaces too.
 
* Works for interfaces too.
 +
 +
<syntaxhighlight lang="java5">
 +
 +
class Animal {
 +
}
 +
 +
class Dog extends Animal {
 +
}
 +
 +
class Cat extends Animal {
 +
}
 +
 +
public class ArrayTest2 {
 +
 +
public static void main(String[] args) {
 +
 +
 +
Dog[] dogs = new Dog[3];
 +
Cat[] cats = {new Cat()};
 +
Animal[] animals = {new Animal(), new Dog(), new Cat()};
 +
 +
animals = dogs;
 +
animals = cats;
 +
 +
}
 +
}
 +
 +
</syntaxhighlight>
  
 
[[Category:OCPJP]]
 
[[Category:OCPJP]]

Revision as of 11:26, 28 June 2011

Introduction

  • Arrays store multiple variables of the same type.
  • The array itself is always an object on the heap (even if it is storing primitive elements).
  • Arrays can be multi-dimensional.
  • Think of a multi-dimensional array as an array of arrays.

Declaring

  • int[] scores; (Preferred) or int scores[]; (Legal, but bad)
  • String[] names;
  • int[][] matrix;

Constructing

  • int[] scores = new int[10];
  • This will create a new array object on the heap. All the int values will be assigned their default values, Object references will be assigned null.
  • The array size must be present, when there is no initializer.
  • double[] rates = new double[]; //illegal
  • Thread[] pool = new Thread[10]; //No thread objects are created here, only the array object to hold ten thread references is created.
  • int[][] matrix = new int[5][];
  • Only the size of the array needs to be specified - in this case 5 says that matrix 2-D array can store 5 1-D array objects. So only the 5 is sufficient to be specified.
  • Of course we can also specify both dimensions like int[][] matrix = new int[3][2]; Which menas that matrix can store 3 1-D arrays each of length 2.

Initializing

  • Initializing means assigning values to the elements of the array.
  • Initializing can be done after the array is declared and constructed.
  • Initializing can also be combined along with the array declaration and construction.
String[] names; //Declare
names = new String[3]; //Construct
//Initialize
names[0] = "A";
names[1] = "B";
names[2] = "C";

//All in one
String[] names = new String[] {"A", "B", "C"};

//Multi-dimensional - initing a 3x3 int array
int[][] matrix = { {1,2,3}, {4,5,6}, {7,8,9} };
Illegal Legal
int[5] scores; //No size in LHS.
int[] scores = new int[]; //No size specified;
int[3] scores = new int[3] {1, 2, 3}; //Cant specify both size and initializer

int[] scores;
scores = {1,2,3}; //Intializer being used, without array being constructed.
int[] scores;
scores = new int[] {1,2,3}; //OK

int[] scores = {1, 2, 3 }; //OK - concise.

int[] scores = new int[] {1, 2 , 3}; /OK

Assignments

  • For primitive array types, once the array is declared it cannot point to a different array type.
  • However, Object of any subclass of the declared array type can be put into the array.
  • It also works for the whole array itself. a subtype array can be assigned to a super-type array.
  • Works for interfaces too.
class Animal {
}

class Dog extends Animal {
}

class Cat extends Animal {
}

public class ArrayTest2 {

	public static void main(String[] args) {
		

		Dog[] dogs = new Dog[3];
		Cat[] cats = {new Cat()};
		Animal[] animals = {new Animal(), new Dog(), new Cat()};		
		
		animals = dogs;
		animals = cats;
		
	}
}