我是 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 你好