2016-09-28 2 views
0

ArrayList를 보유하고있는 클래스가 있습니다. testClass에서 getOrderItems 메서드를 호출하여 배열의 모든 항목을 인쇄 할 수 있기를 바랍니다. 배열 요소가 반환되는 방식에 문제가 있습니다.Java의 ArrayList 요소를 인쇄하십시오.

//email possible & create 
     int emailPossible = card.verifyCard(); 
     if (emailPossible > 0) { 
      System.out.println("Email object has been added"); 
      System.out.println("Your orders was successful and has been placed.\nHere are the details of your order: \n"); 
      System.out.println("Items:\n------"); 
      System.out.println(cart.getOrderItems()); 
     }else{ 
      System.out.println("Email object has not been added"); 
     } 
     //end email possible & create 

하지만, 내 출력이 항목 자체를 각 항목의 주소와하지를 인쇄 할 나타납니다 : 여기

package shopping; 

import java.util.ArrayList; 

public class ShoppingCart { 
    private ArrayList<Item> list; 


    public ShoppingCart() { 
     this.list = new ArrayList<Item>(5); 
    } 

    public void addItem(Item item1) { 
     this.list.add(item1); 
    } 

    public int getTotal() { 
     int total = 0; 

     for(Item it : list) { 
      total = total + it.getCost(); 
     } 
     return total; 
    } 

    public void removeItem(Item item1) { 
     this.list.remove(item1); 
    } 

    public int finalizeOrder() { 
     int cartSize = this.list.size(); 
     return cartSize; 
    } 

    //print elements from ArrayList<Item> 
    public String getOrderItems() { 
     System.out.println(this.list); 
    return null; 
    } 

} 

가의 TestClass에서 블록 : 여기

는 클래스

Email object has been added 
Your orders was successful and has been placed. 
Here are the details of your order: 

Items: 
------ 
[[email protected], [email protected], [email protected]] 
null 
+0

[Object # toString] (https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#toString())을 재정의하거나 인쇄하려는 필드에 액세스하십시오. 방법. – SomeJavaGuy

답변

0

의 경우 toString() 메서드를 재정의해야합니다. 10 클래스. 사용자 정의 방식으로 객체를 인쇄 할 수 있습니다.

0

클래스에서 객체의 수퍼 클래스 메소드를 재정 의하여 toString을 구현하면 목록을 참조가 아닌 문자열로 인쇄 할 수 있습니다.

관련 문제