여러 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 클래스에서이 메서드가 필요하다고 가정하고 있지만 반환 할 대상은 무엇입니까?
당신이하지 않은 이유는)
샘플 코드를
물론은 ... 당신은 그뿐만 아니라
Node#toString
를 재정의해야합니다 : 다음과 같이 할 수'LinkedList' 클래스에서'toString' 메쏘드를 오버라이드 시켰습니다. –