/* ManyNamesHarness.java example code AC Chapin */ package cis214.pkg08arrays; // class demonstrating use of classes with arrays // vs arrays of objects public class ManyNamesHarness { public static void main(String[] args) { // a ManyNames contains an array of strings ManyNames m1 = new ManyNames(); // pass in strings to be added to the array inside the ManyNames m1.addName("Butch"); m1.addName("Sue"); m1.addName("Edgar"); System.out.println(m1.getNthName(2)); // we can do these because the array is public (THIS IS A BAD IDEA) m1.myNames[1] = "Peggy Sue"; System.out.println("Name #0:" + m1.myNames[0]); //m1 = "Bob"; // WRONG, m1 is object (with array of strings) not string //m1[0] = "Bob"; // WRONG m1 is object (with array inside) not array // an array of ManyNames contains addresses // of several ManyNames objects // each of which contains an array of strings // space for the array of ManyNames addresses ManyNames[] mnList = new ManyNames[3]; // point the first array element at the existing ManyNames mnList[0] = m1; // we can do these because the array is public (THIS IS A BAD IDEA) mnList[0].myNames[1] = "Peggy Lou Sue"; System.out.println("Name #0:" + mnList[0].myNames[0]); // mnList[0] = "Bob" // WRONG, mnList[0] is object (with array of strings) not string // mnList[0][0] = "Bob" // WRONG, this is syntax for 2D array // mnList[0].[0] = "Bob" // WRONG just wrong // create a new ManyNames mnList[1] = new ManyNames(); // array element is perfectly good variable // of type ManyNames mnList[1].addName("Agatha"); mnList[1].addName("Francis"); mnList[2] = new ManyNames(); // get the first name in the array of strings inside // 0th ManyNames in the array mnList (counting from 1) // and add it to the array of strings inside // the 2nd ManyNames in the array mnList mnList[2].addName(mnList[0].getNthName(1)); // same for 1st ManyNames in the array mnList mnList[2].addName(mnList[1].getNthName(1)); // really shouldn't be public but... mnList[1].myNames[0] = mnList[0].myNames[2]; System.out.println(mnList[0].getNthName(2)); System.out.println(mnList[1].getNthName(2)); System.out.println(mnList[2].getNthName(2)); } }