파일에서 여러 개인의 이름과 성을 포함하는 파일을 Java 프로그램에 입력하려고합니다. 나는 정보에 접근하기위한 접근 자 (accessor)와 돌연변이 자 (mutators)뿐만 아니라 성과 이름을위한 두 개의 문자열을 가진 People 클래스를 가지고있다. 내 주요 방법 안에, 나는 파일의 끝까지 각 사람을 한 줄씩 가져 오는 while 루프를 가지고있다. 각 라인의 생성자를 통해 Person의 새 인스턴스를 만들고 배열에 복사본을 만드는 것으로 가정합니다. while 루프가 끝났을 때 배열의 내용을 출력 할 때 배열은 파일의 마지막 사람의 정보로 채워진 것 같습니다. 그러나, 만약 내가 String [] values = line.split ("\ t"); Person person = new Person (값 [0], 값 [1]); 두 개의 차원 배열을 사용하여 파일에있는 모든 정보의 사본을 보관하면 잘 작동합니다. 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