2014-11-06 2 views
1

이후에 인쇄됩니다. 파일을 가져 와서 파일을 읽고 배열에 내용을로드하고, 사용자가 파일을 변경하고 그 내용을 찾습니다. 내 문제는 비록 배열을 검색하려고 할 때 항상 메모리에 저장된 내용과 함께 내용을 인쇄하는 것입니다. 나는 그 무엇이 무엇인지 알 수 없기 때문에 그것을 멈추는 법을 이해할 수 없다. 이것은 Directory1 @ 27391d를 통해 인쇄하는 것입니다.Java 배열이 채워지고 값이 인쇄됩니다. 하지만, 메모리는 항상

import java.io.File; 
    import java.io.FileNotFoundException; 
    import java.util.*; 
    public class Directory1{ 


    private int size=0; 
    private String[] dval;//Sting Array 


    public Directory1(String directoryFileName){ 

    int size=0;//counter for the array Size 
    int ct=0;//counter 
    dval = new String[size];//New String array 

    File file= new File(directoryFileName); 

    try{ 
     Scanner insert = new Scanner(file); 
     Scanner populate= new Scanner(file);//Scans File 

     //Counts the amount of lines to get array size 
     while(populate.hasNextLine()){ 
     String i= populate.nextLine(); 
     size++; 
     } 
     dval= new String[size]; 



     //Inserts values into the array 
     for(int i=0;i<dval.length;i++){ 
     String j= insert.nextLine(); 
     dval[ct]= j; 
     System.out.println(dval[ct]); 
     ct++; 


     } 
     }catch(FileNotFoundException e) { 
     e.printStackTrace(); 
     } 
    }//End Constructor 

} 나는 항상 디렉토리의 이름을 인쇄이를 사용하여 초기화뿐만 아니라 메모리에 뭔가를 출력합니다.

public class Directory{ 
    public static void main(String[] args){ 

    Directory1 direct= new Directory1("directory.txt"); 
    System.out.println(direct); 

} 
} 
+0

하나의 경우 배열은 Java에서 크기 조정할 수 없습니다. 그렇다면 왜'Files.readAllLines()'를 사용하지 않으시겠습니까? – fge

답변

2

아마도 생성자에서 직접 인쇄하는 것은 좋지 않습니다. 그러나 [email protected]이 인쇄되는 이유는 개체 자체를 인쇄하려고 시도하기 때문입니다. toString()을 재정의하지 않았으므로 Stringthe inherited toString() method from Object에서 반환되었습니다. ObjecttoString() 메소드를 오버라이드 (override)하고 어떤에서 String을 만들

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

Directory1에서 toString() 방법을 만들기 :

는,이 메소드는 다음의 값과 동일한 문자열을 반환 println 문을 인쇄하여 해당 메서드에서 반환하려고했습니다.

관련 문제