java - 尝试获取 2 个 arrayList 并将它们相乘并将新值存储到新的 arrayList 中以查找最小值/最大值

标签 java arraylist max min

我的代码无法运行,我不知道为什么。这是我收到的错误:

Exception in thread "main" java.util.NoSuchElementException
at java.util.AbstractList$Itr.next(AbstractList.java:350)
at java.util.Collections.max(Collections.java:638)
at Project01.getHighestDollarAmount(Project01.java:120)
at Project01.main(Project01.java:45)

我需要获取 2 个 arrayList(数量、价格)并将这 2 个值相乘并将它们存储在一个新的 arrayList 中,然后找到该 arrayList 的最小值和最大值。

我的代码:

import java.util.*;
import java.io.*;

public class Project01 {

public static void main(String[] args) {
    ArrayList<String> Titles = new ArrayList<String>();//Declare the array lists that will be used.
    ArrayList<String> Types = new ArrayList<String>();
    ArrayList<Double> Prices = new ArrayList<Double>();
    ArrayList<Integer> Quantities = new ArrayList<Integer>();
    ArrayList<Double> Dollars = new ArrayList<Double>();
    int count = 0;//Set the counter to zero.
    Scanner in = new Scanner(System.in);//Establish the scanner so user input can be properly read.
    String database = getFile(in);//Setting the file name variable from the method below that asks the user for the file's name.
    try {
        File file = new File(database);
        Scanner inputFile = new Scanner(file);
        System.out.println();
        System.out.println("Product Summary Report");
        System.out.println("------------------------------------------------------------");
        while (inputFile.hasNextLine()) {
            getTitle(Titles, inputFile.nextLine());
            getQuantity(Quantities, inputFile.nextInt());
            inputFile.nextLine();
            getPrice(Prices, inputFile.nextDouble());
            inputFile.nextLine();
            getType(Types, inputFile.nextLine());
            System.out.println("Title: " + Titles.get(count));
            System.out.println(" Product Type: " + Types.get(count));
            System.out.println(" Price: " + Prices.get(count));
            System.out.println(" Quantity: " + Quantities.get(count));
            System.out.println();
            count++;
        }
        System.out.println("-----------------------------------------------------------------");
        System.out.println("Total products in database: " + count);
        Integer index = getLargestQuantityTitle(Quantities);
        System.out.println("Largest quantity item : " + Titles.get(index) + " (" + Types.get(index) + ")");
        Double highestTotalDollarAmount = getHighestDollarAmount(Dollars);
        System.out.println("Highest total dollar item: $" + highestTotalDollarAmount);
        Integer index2 = getSmallestQuantityTitle(Quantities);
        System.out.println("Smallest quantity item: " + Titles.get(index2) + " (" + Types.get(index2) + ")");
        System.out.println("Lowest total dollar item: ");
        System.out.println("-----------------------------------------------------------------");
        inputFile.close();
    } catch (IOException e) {
        System.out.println("There was a problem reading from " + database);

    }
    in.close();
}
private static String getFile(Scanner inScanner) {
    System.out.print("Enter database filename: ");
    String fileName = inScanner.nextLine();
    return fileName;
}
private static void getTitle(ArrayList<String> Titles, String title) { //This method is creating the array list of the titles from the input file.
    Titles.add(title);
}
private static void getType(ArrayList<String> Types, String type) { //This method is creating the array list of the types from the input file.
    Types.add(type);
}
private static void getPrice(ArrayList<Double> Prices, double price) { //This method is creating the array list of the prices from the input file.
    Prices.add(price);
}
private static void getQuantity(ArrayList<Integer> Quantities, int quantity) { //This method is creating the array list of the quantities from the input file.
    Quantities.add(quantity);
}
private static Integer getLargestQuantityItem(ArrayList<Integer> Quantities){ //This method is determining the maximum value within the quantities array list.
    return Collections.max(Quantities);
    }
private static Double getHighestPricedItem(ArrayList<Double> prices){ //This method is determining the maximum price within the prices array list.
    return Collections.max(prices);
}
private static Integer getHighestTotalDollarItem(ArrayList<Integer> Prices){ //This method is determining the maximum total value, basically the highest quantity of the item multiplied by it's price.
    return Collections.max(Prices);
}
private static Integer getSmallestQuantityItem(ArrayList<Integer> Quantities){ //This method is determining the minimum value within the quantities array list.
    return Collections.min(Quantities);
    }
private static Integer getLargestQuantityTitle(ArrayList<Integer> Quantities){
    int index = 0;
    Integer largestQuantityMainVariable = getLargestQuantityItem(Quantities);
    for (int i = 0; i < Quantities.size(); i++) {
        if (Quantities.get(i) != null && Quantities.get(i).equals(largestQuantityMainVariable)) {
            index = i;
            break;
        }
    }
    return index;
}
private static Integer getSmallestQuantityTitle(ArrayList<Integer> Quantities){
    int index2 = 0;
    Integer smallestQuantityMainVariable = getSmallestQuantityItem(Quantities);
    for (int i = 0; i < Quantities.size(); i++) {
        if (Quantities.get(i) != null && Quantities.get(i).equals(smallestQuantityMainVariable)) {
            index2 = i;
            break;
        }
    }
    return index2;
}
private static ArrayList<Double> Dollars (ArrayList<Integer> Quantities, ArrayList<Double> Prices){
    int counter=0;
    while (counter<Quantities.size()){
        ArrayList<Double> Dollars = new ArrayList<Double>();
        Dollars.add(Quantities.get(counter)*Prices.get(counter));
    counter++;
    }

    return Dollars(null, null);
}
private static Double getHighestDollarAmount(ArrayList<Double> Dollars){ //This method is determining the maximum price within the prices array list.
    return Collections.max(Dollars);
}

}

我的输出:

Enter database filename: /Desktop/proj1_input

Product Summary Report
------------------------------------------------------------
Title: The Shawshank Redemption
Product Type: DVD
Price: 19.95
Quantity: 100

Title: The Dark Knight
Product Type: DVD
Price: 19.95
Quantity: 50

Title: Casablanca
Product Type: DVD
Price: 9.95
Quantity: 137

Title: The Girl With The Dragon Tattoo
Product Type: Book
Price: 14.95
Quantity: 150

Title: Vertigo
Product Type: DVD
Price: 9.95
Quantity: 55

Title: A Game of Thrones
Product Type: Book
Price: 8.95
Quantity: 100

-----------------------------------------------------------------
Total products in database: 6
Largest quantity item : The Girl With The Dragon Tattoo (Book)
Exception in thread "main" java.util.NoSuchElementException
at java.util.AbstractList$Itr.next(AbstractList.java:350)
at java.util.Collections.max(Collections.java:638)
at Project01.getHighestDollarAmount(Project01.java:120)
at Project01.main(Project01.java:45)

输入文件(.txt 文件):

The Shawshank Redemption
100
19.95
DVD
The Dark Knight
50
19.95
DVD
Casablanca
137
9.95
DVD
The Girl With The Dragon Tattoo
150
14.95
Book
Vertigo
55
9.95
DVD
A Game of Thrones
100
8.95
Book

最佳答案

我明白你哪里有问题了。您正在每次迭代时在 Dollars 方法中初始化 ArrayList。 更改此代码

while (counter<Quantities.size()){
        ArrayList<Double> Dollars = new ArrayList<Double>();
        Dollars.add(Quantities.get(counter)*Prices.get(counter));
        counter++;
    }

至此

ArrayList<Double> Dollars = new ArrayList<Double>();

    while (counter<Quantities.size()){
            Dollars.add(Quantities.get(counter)*Prices.get(counter));
        counter++;
        }

所以最终的方法应该是这样的

private static ArrayList<Double> toDollars(ArrayList<Integer> quantities, ArrayList<Double> prices){
int counter=0;
ArrayList<Double> outputInDollars= new ArrayList<Double>();
while (counter<quantities.size()){
    outputInDollars.add(quantities.get(counter)*prices.get(counter));
counter++;
}

return outputInDollars;
}

我也推荐你使用for循环,但这取决于你。尝试使用驼峰命名变量

关于java - 尝试获取 2 个 arrayList 并将它们相乘并将新值存储到新的 arrayList 中以查找最小值/最大值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28055971/

相关文章:

java - hashmap 与 arraylist 的组合用于制作复杂的结构

java - 具有相同内容的两个 ArrayList 在 JUNIT 中不匹配

java - 这两种从现有列表创建新 ArrayList 的方法有什么区别

python - 提取嵌套字典中具有最高值的键

java - 如何在 Windows 注册表中添加新变量

java - 如何从开放位置代码中获取完整代码

java - Tomcat集群环境的架构问题

JavaFX - 切换到新场景时出现 NullPointerException

mysql 最大严格模式

c++ - 如何从 vector<std::string> 获取 max_element