ArrayList of arrays继续覆盖我之前的数组,并添加了最新的数组

时间:2011-12-28 16:22:34

标签: java xml static replace arraylist

我正在解析xml文件。我的XML处理程序,我的对象包含数组的Arraylist,以及运行所有内容并打印它的主类。问题是,每次我将一个数组添加到我的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();
}

}

2 个答案:

答案 0 :(得分:2)

实际上,添加下一个值时,List的值不会更改,但更改了数组part。我的意思是,List在每个位置都有一个引用相同的数组,你可以调用它。换句话说,List实际上并不复制对象,而是存储引用该对象的东西(将其视为“变量”)。

很自然地,如果您更改List在任何索引处引用的对象,当您退出List时,您将看到这些更改。要解决此问题,请在每次使用new关键字显式添加到列表时创建一个新数组(或者每次添加时都可以clone数组)。

答案 1 :(得分:2)

您正在重复使用part,即您多次添加它,但会覆盖其内容。 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;
}