填充的ArrayList這是我的方法:爲什麼我不能在Java中
public void Read() {
try{
File fXmlFile = new File("C://Students.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName("student");
Student s = new Student(); // Creating a new object
for (int i = 0; i < nList.getLength(); i++) {
Node nNode = nList.item(i);
if (nNode.getNodeType() == Node.ELEMENT_NODE){
Element eElement = (Element) nNode;
s.First_Name = eElement.getElementsByTagName("firstname").item(0).getTextContent();
Student.Last_Name = eElement.getElementsByTagName("lastname").item(0).getTextContent();
Student.ID = eElement.getAttribute("id");
Student.Spec = eElement.getElementsByTagName("spec").item(0).getTextContent();
Student.Course = eElement.getElementsByTagName("course").item(0).getTextContent();
Student.Ratingstr = eElement.getElementsByTagName("rating").item(0).getTextContent();
students.add(new Student());
}
}
}catch(Exception e){}
}
public static void main(String argv[]){
Student y = new Student();
y.Read();
}
,這些都是我的類字段:
ArrayList<Student> students = new ArrayList<>();
public static String First_Name;
public static String Last_Name;
public static String ID;
public static String Spec;
public static String Course;
public static String Ratingstr;
public float Rating;
爲什麼我填充第二 「S」 -object或第二個學生,但不是第一個? 我的意思是當我在第一個週期中用第一個學生的第一個對象寫第一個索引時,我需要用第二個學生填充第二個索引,但是我只填寫第一個,第二個索引僅用第二個學生。我在第二個循環的第一個索引上寫道。這是爲什麼?
你的問題不清楚。你能否重新設計它以明確你需要什麼? –
這篇文章看起來更像是手機上的短信,而不是SO上的問題。 –
不要編輯你的問題,像這樣.... http://stackoverflow.com/help/asking –