java中orglist的什么意思_将ArrayList分配给java中的List

我正在实现可以考虑以下junit测试的代码:

package it.unica.pr2.pizze.test;

import static org.junit.Assert.assertEquals;

import static org.junit.Assert.fail;

import static org.junit.Assert.assertTrue;

import org.junit.Test;

import org.junit.Ignore;

import org.junit.runner.RunWith;

import org.junit.runners.JUnit4;

import java.util.*;

import it.unica.pr2.pizze.*;

@RunWith(JUnit4.class)

public class TestPizza {

@Test

public void test1() {

Ingrediente mozzarella = new Ingrediente("mozzarella",50);

Ingrediente pomodoro = new Ingrediente("pomodoro",10);

Ingrediente[] ingredienti = new Ingrediente[] {mozzarella, pomodoro}

Pizza pizzaMargherita = new Pizza(ingredienti);

assertTrue( pizzaMargherita.calorie() == 60 );

List ingredientiMargherita = pizzaMargherita;

assertTrue(ingredientiMargherita.size() ==2);

assertTrue(ingredientiMargherita.get(0) == mozzarella);

assertTrue(ingredientiMargherita.get(1) == pomodoro);

}

这是我的classe:比萨饼

package it.unica.pr2.pizze;

import java.util.ArrayList;

import java.util.List;

public class Pizza {

private ArrayList ingredienti;

public Pizza(Ingrediente[] ing) {

this.ingredienti = new ArrayList<>();

int i = 0;

while (i < ing.length) {

this.ingredienti.add(ing[i]);

i++;

}

}

public double calorie(){

double sumaCalorie = 0;

for(Ingrediente elem: this.ingredienti)

sumaCalorie += elem.getCalorie();

return sumaCalorie;

}

}

和另一类:Ingrediente

package it.unica.pr2.pizze;

public class Ingrediente {

private String nomeIngrediente;

private double calorie;

public Ingrediente(String nomeIngrediente, double calorie) throws IngredienteNonValidoException {

this.nomeIngrediente = nomeIngrediente;

if (calorie < 0) throw new IngredienteNonValidoException();

else

this.calorie = calorie;

}

public void setNomeIng(String nomeIngrediente) {

this.nomeIngrediente = nomeIngrediente;

}

public void setCalorie(double calorie) {

this.calorie = calorie;

}

public String getNomeIng() {

return this.nomeIngrediente;

}

public double getCalorie() {

return this.calorie;

}

}

运行测试后,我收到以下错误:

错误:不兼容的类型:Pizza无法转换为List

列表ingredientiMargherita = pizzaMargherita;

所以我不知道如何使用operator =将ArrayList转换为List,我无法修改junit测试代码.

解决方法:

如果您无法修改作业,则必须执行以下操作:

public class Pizza implements List {

...

}

或类似的东西,比如

public class Pizza extends AbstractList {

...

}

标签:java,list,arraylist,junit

来源: https://codeday.me/bug/20190829/1763549.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值