複数の個人の姓名を含むファイルをファイルから Java プログラムに入力しようとしています。私は、情報にアクセスするためのアクセサーとミューテーターだけでなく、名字と姓の 2 つの文字列を持つ People クラスを持っています。私のメイン メソッド内には、ファイルの最後まで各人物を 1 行ずつ取り込む while ループがあります。行ごとにコンストラクターを介して Person の新しいインスタンスを作成し、配列にコピーすることを想定しています。while ループが終了したら、配列の内容を出力すると、ファイル内の最後の人物の情報で配列が埋められているようです。ただし、 String[] values = line.split("\t"); をコメントアウトすると、and Person child = new Person(values[0], values[1]); 2 次元配列を使用してファイル内のすべての情報のコピーを保持すると、問題なく動作します。People 配列のファイルに含まれるすべての個人の名前のコピーを保持できないという、私が間違っていることはありますか?
public class Person
{
protected static String first;
protected static String last;
private static int id;
public Person(String l, String f)
{
last = l;
first = f;
} // end of constructor
public String getFirst()
{
return first;
} // end of getFirst method
public static String getLast()
{
return last;
} // end of getLast method
public static int getID()
{
return id;
} // end of getLast method
public static void setFirst(String name)
{
first = name;
} // end of setFirst method
public static void setLast(String name)
{
last = name;
} // end of setLast method
public static void setID(int num)
{
id = num;
} // end of setLast method
} // end of Person class
public class Driver
{
public static void main(String arg[])
{
Person[] temp = new Person[10];
try
{
BufferedReader br = new BufferedReader(new FileReader(arg[1]));
String line = null;
int counter = 0;
while ((line = br.readLine()) != null)
{
String[] values = line.split("\t");
Person child = new Person(values[0], values[1]);
temp[counter] = child;
System.out.println("Index " + counter + ": Last: " + child.getLast() + " First: " + child.getFirst());
System.out.println("Index " + counter + ": Last: " + temp[counter].getLast() + " First: " + temp[counter].getFirst() + "\n");
counter++;
}
br.close();
}
catch(Exception e)
{
System.out.println("Could not find file");
}
for(int row = 0; row < 7; row++)
{
System.out.print("Row: " + row + " Last: " + temp[row].getLast() + " First: " + temp[row].getFirst() + "\n");
}
}
} // end of Driver class