我想动态创建对象,先在List创建空对象,然后使用createEquipment复制,返回List,但是不会写了

这里我应该怎么写?

package com.mvc;

import com.mvc.bean.Equipment;

import java.util.ArrayList;

import java.util.List;

public class MingleObject {

public static List<Equipment> createEquipment(List list, String[] names,

int[] amounts){

int j = list.size();

for (int i = 0; i < j; i++) {

list.get(i).setName(names[i]);

list.get(i).setAmount(amounts[i]);

}

return list;

}

public static void main(String[] args) {

List<Equipment> strArray = new ArrayList<Equipment> ();

Equipment switchEqu = new Equipment();

Equipment routerEqu = new Equipment();

strArray.add(switchEqu);

strArray.add(routerEqu); //向上转型

String[] object01={"switchEqu", "routerEqu"}; // 对象变量名称

String[] name01={"switch", "router"};// 属性

int[] amount01={777,123}; // 属性

MingleObject.createEquipment(strArray, name01, amount01);

System.out.println(strArray.get(0).getName());

}

}

package com.mvc.bean;

public class Equipment {

private String name; // 名称

private int amount;// 数量

/**

* 默认的构造方法必须不能省,不然不能解析

*/

public Equipment(){

}

public Equipment(String name, int amount) {

this.name = name;

this.amount = amount;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getAmount() {

return amount;

}

public void setAmount(int amount) {

this.amount = amount;

}

@Override

public String toString() {

return "装备 [名称=" + name +

", 数量=" + amount +

"]";

}

}

图片描述

回答:

public static List<Equipment> createEquipment(List<Equipment> list, String[] names, 

int[] amounts){

int j = list.size();

for (int i = 0; i < j; i++) {

list.get(i).setName(names[i]);

list.get(i).setAmount(amounts[i]);

}

return list;

回答:

你的输入
输出
期望的是什么?

以上是 我想动态创建对象,先在List创建空对象,然后使用createEquipment复制,返回List,但是不会写了 的全部内容, 来源链接: utcz.com/p/179089.html

回到顶部