-2

我是 Java 开发的新手,我尝试了这个程序:

    List<String> myList = new ArrayList<String>();
    for(int i = 0;i<ProductNumber;i++)
    {
       myList.add( mProducts[i].Name);
    }
    mProducts[1].Name = "Hello";

当我println显示myList内容时:

预期的

“A”
“B”
“C”

当前的

>"A"  
>"Hello"  
>"C"  

谁能告诉我发生了什么事?

这是我制作的代码:

    public class xmlParsing 
    {   
    private final static String XML_FILE_NAME = "Configuration.xml";
    private final static String PRODUCT_NODE = "product";
    private final static String TASK_NODE = "task";
    private final static String NAME_ITEM = "name";
    private final static String DURATION_ITEM = "duration";
    private final static String RATE_ITEM = "rate";

    private static int ProductNumber = 0;
    private static int TaskNumber = 0;
    private static Product mProducts[] = new Product[Product.MAX_PRODUCT_NUMBER];

    public xmlParsing()
    {
    }

    public void parseXML ()
    {
        Product[] Products = new Product[Product.MAX_PRODUCT_NUMBER];
        try 
        {
            File xmlFile = new File(XML_FILE_NAME);
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(xmlFile);
            doc.getDocumentElement().normalize();
            NodeList productsNode = doc.getElementsByTagName(PRODUCT_NODE);
            ProductNumber = productsNode.getLength();
            Product mproduct = new Product();
            Task mtask = new Task();
            for (int i = 0; i < productsNode.getLength(); i++) 
            {
                Node productNode = productsNode.item(i);
                if (productNode.getNodeType() == Node.ELEMENT_NODE) 
                {
                    Element element1 = (Element) productNode;
                    mproduct.Name = getValue(NAME_ITEM, element1);
                    NodeList tasksNode = element1.getElementsByTagName(TASK_NODE);
                    TaskNumber = tasksNode.getLength();
                    Task[] Tasks = new Task[Task.MAX_TASKS_NUMBER];
                    for (int j = 0; j < tasksNode.getLength(); j++) 
                    {
                        Node taskNode = tasksNode.item(j);
                        if (taskNode.getNodeType() == Node.ELEMENT_NODE) 
                        {
                            Element element2 = (Element) taskNode;
                            mtask.Name = getValue(NAME_ITEM, element2);
                    mtask.Duration = Double.parseDouble(getValue(DURATION_ITEM, element2));
                            mtask.Rate = Double.parseDouble(getValue(RATE_ITEM, element2));
                            Tasks[j] = new Task(mtask);
                        } 
                    }
                    mproduct.Tasks = Tasks;
                    Products[i] = new Product(mproduct);
                }
            }
        } 
        catch (Exception ex) 
        {
            ex.printStackTrace();
        }
        mProducts = Products;
    }

    private static String getValue(String tag, Element element) 
    {
        NodeList nodes = element.getElementsByTagName(tag).item(0).getChildNodes();
        Node node = (Node) nodes.item(0);
        return node.getNodeValue();
    }

    public static List<String> getProdcutNames() 
    {
       String ps[] = new String[ProductNumber];
       List<String> myList = new ArrayList<String>();
       for(int i = 0;i<ProductNumber;i++)
       {
           ps[i] =  mProducts[i].Name;
           myList.add(new String(mProducts[i].Name));
       }
        mProducts[2].Name = "Hello";
       return myList;
    }

    public int getProductNumber()
    {
        return ProductNumber;
    }

    public int getTasktNumber()
    {
        return TaskNumber;
    }
    }

    public class Task {
    public static final int MAX_TASKS_NUMBER = 100;
    public String Name;
    public double Duration;
    public double Rate;

    public Task(){
        Name = "";
        Duration = 0;
        Rate = 0;
    }

    public Task(Task t){
        Name = t.Name;
        Duration = t.Duration;
        Rate = t.Rate;
    }

    }

    public class Product {
    public static final int MAX_PRODUCT_NUMBER = 200;
    public String Name;
    public Task[] Tasks;

    public Product(){
        Name = "";
        Tasks = null;
    }

    public Product(Product p){
        Name = p.Name;
        Tasks = p.Tasks;
    } 
    }


    public class Production {

    public static void main(String[] args) {
        xmlParsing parser = new xmlParsing();
        parser.parseXML();
        for(int i=0;i<parser.getProductNumber();i++)
            System.out.println(parser.getProdcutNames().get(i));

    }
    }

xml文件是

    <product>
    <name>1510</name>
    <task>
        <name>DECOUPAGE PRESSE 250T</name>
        <duration>1</duration>
        <rate>144</rate>
    </task>
    <task>
        <name>LAMINAGE PARBOLIQUE 1</name>
        <duration>0</duration>
        <rate>72.1</rate>
    </task>
     </product>
    <product>
    <name>2020</name>
    <task>
        <name>DECOUPAGE PRESSE 250T</name>
        <duration>1</duration>
        <rate>144</rate>
    </task>
    <task>
        <name>LAMINAGE PARBOLIQUE 1</name>
        <duration>0</duration>
        <rate>72.1</rate>
    </task>
     </product>

预计 : 1510 2020 当前 1510 你好

4

3 回答 3

2

它看起来好像mProducts并且myList指的是同一个对象,但是在您提供的代码中,这不可能发生。您是否简化了代码,删除了类似的内容mProducts = myList

于 2013-05-07T21:40:46.910 回答
0

感谢您发布完整的代码。问题是您多次调用getProdcutNames()(顺便说一句,您的名称中有错字)main()- 每次在循环中再次main调用getProdcutNames()

for(int i=0;i<parser.getProductNumber();i++)
    System.out.println(parser.getProdcutNames().get(i));

因此,第一次调用时getProdcutNames()mProducts会通过 line 进行修改mProducts[2].Name = "Hello";,然后当getProdcutNames()再次调用时,您将从修改后的版本复制到myList.

最简单的修复,也使代码更高效,是只调用getProdcutNames()一次main()

List<String> names = parser.getProdcutNames();
for(int i = 0; i < names.size() ;i++)
    System.out.println(names.get(i));
于 2013-05-08T06:09:08.927 回答
0

像这样更改您的代码,它应该可以达到目的

List<String> myList = new ArrayList<String>();
for(int i = 0;i<ProductNumber;i++)
{
   myList.add( new String(mProducts[i].Name));
}
mProducts[1].Name = "Hello";
于 2013-05-07T21:56:16.637 回答