gpt4 book ai didi

java - 对具有多种类型的ArrayList进行求和

转载 作者:行者123 更新时间:2023-12-02 09:14:58 27 4
gpt4 key购买 nike

我在总结我在类 Treasureroom 中创建的 ArrayList 时遇到了麻烦,该类位于我的类 Accountant 中。我有一个 Valuable 类型的 ArrayList,如下面的代码所示。我需要能够总结 Accountant 类中的所有 int 元素。

会计师

package Accountant;
import java.util.Iterator;
import java.util.Random;

import ValuableFactory.Valuable;
import rw.RWMonitor;
import rw.Treasureroom;

public class Accountant implements Runnable {
private Treasureroom treasureroom;
private static final int MAX_WAIT_TIME = 10000; // 3 seconds
private static final int MAX_WORK_TIME = 5000; // 2 seconds

private static final Random random = new Random();
private int no;
private RWMonitor monitor;
private Treasureroom resource;

public Accountant( int no, RWMonitor monitor, Treasureroom resource ) {
this.no = no;
this.monitor = monitor;
this.resource = resource;
}

public void run() {
while(true) {
try {
Thread.sleep(random.nextInt(WAIT_TIME));
}
catch (InterruptedException ex) {
}

System.out.println("Accountant counts: " + resource);

monitor.acquireRead();

//System.out.println("Accountant counted: " + valuable);

try {
Thread.sleep(random.nextInt(MAX_WAIT_TIME));
}
catch (InterruptedException ex) {
}

for (int i = 0; i > resource.)
System.out.println("Accountant counted: " + resource.getValueRead());

monitor.releaseRead();

try {
Thread.sleep(random.nextInt(WAIT_AGAIN));
}
catch (InterruptedException ex) {
// TODO: handle exception
}
}
}

//System.out.println("The total sum of TreasureRoom is " + Thread.currentThread().getName());
//TreasureRoom.releaseRead();

}

宝库

package rw;

import java.util.*;
import ValuableFactory.*;

public class Treasureroom {
ArrayList<Valuable> valuables = new ArrayList<Valuable>();

public Valuable getValueWriter() {
return valuables.remove(0);
}

public void updateValue(Valuable valuable) {
valuables.add(valuable);
}

public boolean removeValue(Valuable valuable) {
return valuables.remove(valuable);
}

public Valuable getValueReade() {
for (int i=0; i<valuables.size();i++) {
return valuables.get(i);
}
return null;
}
}

有值(value)

package ValuableFactory;

public class Valuable implements ValuableFactory {
private String name;
private int value;

public Valuable(String name, int value) {
this.name = name;
this.value = value;
}

@Override
public String getName() {
return name;
}

@Override
public int getValue() {
return value;
}
}

最佳答案

您的 Accountant 类在其构造函数中实例化您的 Treasureroom 对象,这意味着要循环访问 Treasureroom 内的 ArrayList code> 对象使用 Accountant 对象,您需要在 Accountant 中调用 Treasureroom 中的方法。

会计师

public int getTreasureSum()
{
return resource.sumTreasure();
}

宝库

public int sumTreasure()
{
int sum = 0;

for(int i = 0; i < valuables.size(); i++)
{
sum+= valuables.get(i).getValue();
}

return sum;
}

此外,在您的 Accountant 类中,您还有一个未使用的 Treasureroom 对象

private Treasureroom treasureroom; 

关于java - 对具有多种类型的ArrayList进行求和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59071420/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com