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);
}
}
首先,數組在Java中不能調整大小;那麼,爲什麼不使用'Files.readAllLines()'? – fge 2014-11-06 17:42:04