Untitled
unknown
plain_text
a year ago
1.3 kB
2
Indexable
Never
public class KennelArray { public static void main (String [] args) { //remember how a "plain" array worked if you want to specify the values int [] nums = {1, 5, 7}; Dog [] dogs = { new Dog("Fido", 2), new Dog("Fifi", 3), new Dog("Cujo", 6) }; //same as: //Dog[] dogs = new Dog[3]; // dogs[0] = new Dog("Fido", 2); // dogs[1] = new Dog("Fifi", 3), // dogs[2] = new Dog("Cujo", 6) for (int i = 0; i < dogs.length -1; i++) System.out.println(dogs[i]); //remember how to declare an array with space for 10 integers int[] moreNums = new int[10]; //Declare one dog whose name is Gypsy who is 3 years old //Write code that changes Gypsy's name to Gypsy Belle. //Write a statement to print Gypsy's personYears //reserve space for 10 references to Dog objects called myDogs //this is just like declaring 10 integers except the data type is Dog instead of int //note this does NOT actually create the objects //each one must be instantiated separately //instantiate a new object for JoJo age 2 in the first array slot //take off the comments and see that this won't work since the object hasn't been created at index 1 //myDogs[1].setName("Karma"); //but this will //JoJo had a birthday //myDogs[0].setAge(3); //System.out.println(myDogs[0]); } //end main } //end class