gpt4 book ai didi

java - 数组的 ArrayList 不断用添加的最新数组覆盖我以前的数组

转载 作者:行者123 更新时间:2023-11-29 03:51:21 25 4
gpt4 key购买 nike

我正在解析一个 xml 文件。我的 XML 处理程序,我的包含数组 Arraylist 的对象,以及运行所有内容并打印它的主类。问题是,每次我将一个数组添加到我的数组列表时,它会将所有先前添加的数组更改为与当前相同的数组。我认为这只是一个静态问题,但一旦我从所有内容中去除静态问题,它仍然在做同样的事情。请帮忙,我需要尽快完成这件事。

这是我的处理程序:

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class MyXMLHandler extends DefaultHandler {

public int counter = 0;
public String[] part = new String[4];
Boolean currentElement = false;
String currentValue = null;
public SitesList sitesList = null; /this used to be static

public SitesList getSitesList() { //this used to be static
return sitesList;
}

public void setSitesList(SitesList sitesList) { //this used to be static
MyXMLHandler handle = new MyXMLHandler(); //thats why the object
handle.sitesList = sitesList;
}

/**
* Called when tag starts ( ex:- <name>text</name> -- <name> )
*/
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {

currentElement = true;

if (localName.equals("string-array")) {
/** Start */

String attr = attributes.getValue("name");
sitesList = new SitesList(attr);
}

}

/**
* Called when tag closing ( ex:- <name>text</name> -- </name> )
*/
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
currentElement = false;

/** set value */
if (counter == 4) {
sitesList.addPart(part);
counter = 0;
}
if (localName.equalsIgnoreCase("item")) {
part[counter] = currentValue;
counter++;
}
currentValue = "";

}

/**
* Called to get tag characters ( ex:- <name>text</name> -- to get
* text Character )
*/
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {

if (currentElement) {
currentValue = new String(ch, start, length);
currentElement = false;
}

}

}

这是我的 SitesList 对象

import java.util.ArrayList;

/** Contains getter and setter method for varialbles */
public class SitesList {

/** Variables */
private ArrayList<String[]> part = new ArrayList<String[]>();

/**
* In Setter method default it will return arraylist change that to add
*/
public SitesList(String c) {
String[] comp = new String[1];
comp[0] = c;
part.add(comp);
// company name is part(0)[0]
}

public String getCompany() {
return this.part.get(0)[0];
}

public ArrayList<String[]> getPart() {
return part;
}

public void addPart(String[] name) {
part.add(name);
}

public String getName(int i) {
return this.part.get(i)[0];
}

public String getComp1(int i) {
return this.part.get(i)[1];
}

public String getComp2(int i) {
return this.part.get(i)[2];
}

public String getComp3(int i) {
return this.part.get(i)[3];
}

public int getSize() {
return this.part.size();
}

}

最佳答案

您正在重用 部分,即您多次添加它但覆盖了它的内容。 ArrayList 在这里是无辜的:)

将添加部分改为:

if (counter == 4) {
sitesList.addPart(part);
//create a new array
part = new String[4];
counter = 0;
}

或者,从 Java 6 开始:

if (counter == 4) {
//add a copy to the list
sitesList.addPart(Arrays.copyof(part, part.length));
counter = 0;
}

关于java - 数组的 ArrayList 不断用添加的最新数组覆盖我以前的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8658455/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com