0

this is partly homework I've been given some code to use and I am trying to figure out one part of it...

   public PhoneAccount[] getAllAccounts() {
      return tm.values().toArray(new PhoneAccount[tm.size()]);
   }

I am trying to find out how to read this in my main method, I've created the instance of the class this is. Which I named "am"

What exactly do I need to do to make it list the contents of the array?

am.getAllAccounts() 

is what it is, just not sure how do I make it print its contents?

4 Answers 4

1

Suppose you have totalBill property in PhoneAccount , then you can print it using the below code.

PhoneAccount[] phoneaccounts = am.getAllAccounts() ;
for( PhoneAccount phoneaccount  : phoneaccounts)
{
 System.out.println(phoneaccount.getTotalBill());
// fetch all the  other properties from phoneaccoun similarly.
}
Sign up to request clarification or add additional context in comments.

1 Comment

This one is perfect! Thank you very much :)
1

You can do the following assuming your PhoneAccount has a sensible toString() implementation.

System.out.println(Arrays.toString(am.getAllAccounts()));

Comments

0

Do you have a proper .toString() method in your PhoneAccount-Class? And what values do you want to get? It would be easy to use a foreach-loop to get the informations and put these in a new array which is returned

Comments

0

Since you have an object called PhoneAccount, you should create a new method inside that which prints out the contents, which you call from your main. Without seeing more detail on the class it's difficult to tell, but probably the simplest way would be something like:

public void printArray(){
   System.out.println(myArray.toString());
}

This will print to the console. Alternatively you could add a loop within the method to print out each array element individually.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.