Java 如何通过获取更改的方法运行更改对象的整个arrayList
好的,所以我正在研究的程序需要创建一个至少1000个变更对象的arrayList,然后计算由该数量产生的变更,就像在销售点环境中一样。我所有的东西都在工作,除了我被如何计算变化的问题难倒了。我在另一个类中编写了一些方法来实现这一点,但我不知道如何将整个arrayList传递给它们来实现这一点。如果有人能帮我这个忙,我将不胜感激 更改类别:Java 如何通过获取更改的方法运行更改对象的整个arrayList,java,arrays,arraylist,Java,Arrays,Arraylist,好的,所以我正在研究的程序需要创建一个至少1000个变更对象的arrayList,然后计算由该数量产生的变更,就像在销售点环境中一样。我所有的东西都在工作,除了我被如何计算变化的问题难倒了。我在另一个类中编写了一些方法来实现这一点,但我不知道如何将整个arrayList传递给它们来实现这一点。如果有人能帮我这个忙,我将不胜感激 更改类别: public class Change { private double amount, remainingAmount; private int occu
public class Change {
private double amount, remainingAmount;
private int occurences;
public Change() {
super();
this.amount = 17.87;
this.remainingAmount = (int)(amount * 100);
}
public Change(double amount, double remainingAmount) {
super();
this.amount = amount;
this.remainingAmount = remainingAmount;
}
public Change(float nextChange) {
this.amount = amount;
this.remainingAmount = remainingAmount;
}
public double getAmount() {
return amount;
}
public void setAmount(double amount) {
this.amount = amount;
}
public double getRemainingAmount() {
return remainingAmount;
}
public void incrementOccurence() {
occurences++;
}
public void setRemainingAmount(double remainingAmount) {
this.remainingAmount = remainingAmount;
}
public double numberOfOneDollars() {
int numberOfOneDollars = (int) (remainingAmount / 100);
remainingAmount = remainingAmount % 100;
return numberOfOneDollars;
}
public double numberOfQuarters() {
int numberOfQuarters = (int) (remainingAmount / 25);
remainingAmount = remainingAmount % 25;
return numberOfQuarters;
}
public double numberOfDimes() {
int numberOfDimes = (int) (remainingAmount / 10);
remainingAmount = remainingAmount % 10;
return numberOfDimes;
}
public double numberOfNickels() {
int numberOfNickels = (int) (remainingAmount / 5);
remainingAmount = remainingAmount % 5;
return numberOfNickels;
}
public int numberOfPennies() {
int numberOfPennies = (int) remainingAmount;
return numberOfPennies;
}
@Override
public String toString() {
return "Change [amount=" + amount + ", remainingAmount="
+ remainingAmount + "]\n";
}
}
更改ArrayList类:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Scanner;
public class ChangeArrayList {
private ArrayList<Change> changeArray = new ArrayList<Change>();
private static int numOfChangeObjects = 1000;
private static String FILE_NAME = "changeData.dat";
public static void makeChangeData() {
PrintWriter outputStream = null;
try
{
outputStream = new PrintWriter(FILE_NAME);
}
catch(FileNotFoundException e) {
System.out.println("Error opening the file " + FILE_NAME);
System.exit(0);
}
for (int count = 0; count < numOfChangeObjects; count++) {
//get random number between 0-1, move decimal right two places, typecast to float
//to get rid of decimals, then divide by 10.0 to get decimal left one place
//then add 3 to make sure numbers are >3
DecimalFormat df = new DecimalFormat("#.##");
double changeData = (float)(Math.random() * 100)/10.0 + 3;
double twoDecimal = Double.valueOf(df.format(changeData));
outputStream.println(twoDecimal + " ");
}
outputStream.close();
System.out.println("Those lines were written to " + FILE_NAME);
}
public void makeChangeArray(String fileName) {
changeArray = new ArrayList<Change>();
Scanner inputStream = null;
try
{
inputStream = new Scanner(new File(FILE_NAME));
}
catch(FileNotFoundException e)
{
System.out.println("Error opening the file " +
FILE_NAME);
System.exit(0);
}
while (inputStream.hasNext())
{
//read in a change object from the file
float nextChange = inputStream.nextFloat();
//Stuck here. Can't figure out what to put in to make this work
//everything else works except this. My change keeps coming out as 0
changeArray.add(new Change(nextChange));
//Stuck here. Can't figure out what to put in to make this work
//everything else works except this. My change keeps coming out as 0
}
inputStream.close();
}
public void writeToFile() {
String fileName = "out.txt"; //The name could be read from
//the keyboard.
PrintWriter outputStream = null;
try
{
outputStream = new PrintWriter(fileName);
}
catch(FileNotFoundException e)
{
System.out.println("Error opening the file " +
fileName);
System.exit(0);
}
outputStream.println(toString());
outputStream.close( );
System.out.println("Those lines were written to " +
fileName);
}
public String toString() {
String s = "";
for (int i = 0; i < changeArray.size(); i++) {
s += changeArray.get(i).toString(); }
return s;
}
public static void main(String[] args) {
ChangeArrayList.makeChangeData();
ChangeArrayList tester = new ChangeArrayList();
tester.makeChangeArray("changeData.dat");
//Something should go here to calculate change
//Not sure how to go about doing this
tester.writeToFile();
}
}
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.io.PrintWriter;
导入java.text.DecimalFormat;
导入java.util.ArrayList;
导入java.util.Scanner;
公共类变更列表{
私有ArrayList changeArray=新ArrayList();
私有静态int numOfChangeObjects=1000;
私有静态字符串文件\u NAME=“changeData.dat”;
公共静态void makeChangeData(){
PrintWriter outputStream=null;
尝试
{
outputStream=新的PrintWriter(文件名);
}
catch(filenotfounde异常){
System.out.println(“打开文件时出错”+文件名);
系统出口(0);
}
对于(int count=0;count
方法1:
您可以在ChangeArrayList
中为changeArray
创建getter方法。在main方法中调用makeChangeArray()
方法后,可以调用getter方法并将返回的arraylist作为参数传递给所需的方法
方法2:我更喜欢这种方法 不要声明类级别的changeArray,而是在
makeChangeArray()
方法中声明它,并将返回类型更改为List
而不是void
,然后返回准备好的arraylist