I will paste my code below, but what I have so far is an array declared, named it "book", did some loops to get some user input and then display the outcome, and that works fine. I was told when you are copying an array, you create a new object and that's where I am stuck. I don't understand how to create and initialize this new object in order to copy my array. I hope I have the right idea at least lol.
I have a portion of it commented out while I was working with this.
import java.util.ArrayList;
import javax.swing.JOptionPane;
/* Exam Practice # 2
* 10.20.2011
* This file loads the array list with PhoneBookEntry objects, and displays the
* content.
*/
public class PhoneBookDemo
{
public static void main(String[] args)
{ String name;
String phoneNumber;
String input;
int inData;
//Creates an array list that holds phonebookentrys, the name of it is called book
ArrayList < PhoneBookEntry > book = new ArrayList < PhoneBookEntry > ();
//bookPhone is my second object
PhoneBookEntry bookPhone;
do//assure input is greater than 0
{
input = JOptionPane.showInputDialog("How many phone book listings to enter");
inData = Integer.parseInt(input);
}
while(inData < 1);
//load the array list
for (int i = 0; i < inData; i++)
{
name = JOptionPane.showInputDialog(null,
"Enter the name:", "Entry" + (i + 1) +
JOptionPane.PLAIN_MESSAGE);
phoneNumber = JOptionPane.showInputDialog(null,
"Enter the phone number:", "Entry" + (i + 1) +
JOptionPane.PLAIN_MESSAGE);
//Primary phonebook object (named it book)
book.add(new PhoneBookEntry(name, phoneNumber));
}
//Create a loop that gets objects from the other class file and maybe switch them around
System.out.println("Name" + "\tPhone Number" );
for (int i = 0; i < book.size(); i++)
{
System.out.println(book.get(i).getName() + "\t" + book.get(i).getPhoneNumber());
}
//Secondary phonebook object for switching
//Use methods from the Array list to switch, or replace
//Print out a paper for each one so you know they all work
//Refer to Page 433 for "Copying Arrays"
//http://download.oracle.com/javase/6/docs/api/java/util/ArrayList.html
//http://download.oracle.com/javase/6/docs/api/java/util/Arrays.html
bookPhone.add(new PhoneBookEntry(name, phoneNumber));
//**********************Secondary phonebook object****************************\\
//PhoneBookEntry bookPhone;
/*do//assure input is greater than 0
{
input = JOptionPane.showInputDialog("How many phone book listings to enter");
inData = Integer.parseInt(input);
}
while(inData < 1);
//load the array list
for (int i = 0; i < inData; i++)
{
name = JOptionPane.showInputDialog(null,
"Enter the name:", "Entry" + (i + 1) +
JOptionPane.PLAIN_MESSAGE);
phoneNumber = JOptionPane.showInputDialog(null,
"Enter the phone number:", "Entry" + (i + 1) +
JOptionPane.PLAIN_MESSAGE);
//Secondary phonebook object (named it bookPhone)
bookPhone.add(new PhoneBookEntry(name, phoneNumber));
}
//Create a loop that gets objects from the other class file and maybe switch them around
System.out.println("Name" + "\tPhone Number" );
for (int i = 0; i < book.size(); i++)
{
System.out.println(book.get(i).getName() + "\t" + book.get(i).getPhoneNumber());
}*/
}
}


Sign In
Create Account

Back to top









