2014-03-25 2 views
1

여러 LinkedList 객체가있는 배열 목록이 있습니다. 내 주요 코드는 다음과 같습니다 ArrayList toString() 메서드

import java.io.*; 
import java.util.AbstractList; 
import java.util.*; 

public class Sort { 
public static void main (String[] args){ 
    int listNum = 0; 
    File file = new File("input.txt"); 

    ArrayList<LinkedList> my_lists = new ArrayList<LinkedList>(); 

    try { 
     Scanner sc = new Scanner(file); 
     while (sc.hasNextLine()) { 

      System.out.println("List" + listNum); 
      String line = sc.nextLine(); 
      LinkedList the_list = new LinkedList(); 
      String[] templist = line.split("\\s*,\\s*"); 

      for(int i=0; i<templist.length; i++){ 
       String temp = templist[i]; 
       the_list.add(temp); 
       System.out.println(temp); 
      } 
      listNum++; 
      my_lists.add(the_list); 


     } 
     sc.close(); 
    } 
    catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } 

    for(int i=0;i<my_lists.size();i++){ 
     System.out.println(my_lists.get(i).toString()); 
     } 
    } 

} 

내 LinkedList의 객체 클래스입니다 :

package kruskal; 
import java.util.AbstractList; 

public class LinkedList { 

Node head; 

public LinkedList(){ 
    this.head = null; 
} 

public void add (Object newData){ 
    Node cache = head; 
    Node current = null; 

    if (cache == null) 
     current = new Node(newData, null); 
     else { 


    while ((current = cache.next) != null) 
     cache = cache.next; 

    cache.next = new Node(newData,null); 
     } 
} 



public Object getFront(){ 
    return this.head.data; 
} 

} 

그리고 내 노드 클래스 :

package kruskal; 

public class Node { 
public Object data; 
public Node next; 

public Node(Object data,Node next){ 
    this.data=data; 
    this.next=next; 
} 
} 

코드 내 주요 부분의 끝에서, 나는 시도 내 ArrayList 내 각 LinkedList를 표시하는 for 루프를 수행하지만 LinkedLists의 주소 만 가져오고 있습니다.

[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 

내 toString() 메서드가 작동하지 않는 이유는 무엇입니까? 내 LinkedList 클래스에서이 메서드가 필요하다고 가정하고 있지만 반환 할 대상은 무엇입니까?

+0

당신이하지 않은 이유는)

샘플 코드를

물론
@Override public String toString() { String res = "["; Node current = this.head; while(current != null) { res += current.toString() + "\t"; current = current.next; } res += "]"; return res; } 

은 ... 당신은 그뿐만 아니라 Node#toString를 재정의해야합니다 : 다음과 같이 할 수
'LinkedList' 클래스에서'toString' 메쏘드를 오버라이드 시켰습니다. –

답변

5

LinkedList 클래스에 toString() 메서드가 정의되어 있지 않으므로 표시되는 출력을 담당하는 the toString() method from Object을 상속해야합니다. 즉

,이 메소드는 다음의 값과 동일한 문자열을 반환

getClass().getName() + '@' + Integer.toHexString(hashCode()) 

무시 toString()LinkedList의를하고 String 당신이 인쇄보고 싶어 돌아갑니다.

+0

내 ArrayList (my_lists [i])에 포함 된 지정된 (i) LinkedList를 반환 할 수있게하려고합니다. 나는 단지 my_lists [i]를 반환 할까? – mickdeez

2

너는 의 toString 방법을 무시해야합니다.

@Override 
public String toString() { 
    return data.toString(); 
} 
+0

이제 널 포인터 예외가 발생합니다. 내 출력물은 여전히 ​​System.out.println이어야합니다 (my_lists.get (i) .toString()); ? – mickdeez

+0

예,이 전화를 계속 사용하십시오! 실제로 뭔가 잘못되었습니다. 방금 대답을 업데이트했습니다. 새 버전을 사용해 볼 수 있습니까? –