如何将 ArrayList<String> 中的所有值相加或转换为 ArrayList<Integer>
How to add up all the values in an ArrayList<String> or convert to ArrayList<Integer>
我正在尝试将 ArrayList 中的所有值相加,但无法得出总和。我必须找到从文本文件中提取的数字的平均值。
public static void main(String[] args) throws IOException {
File file = new File("C:\Users\[REDACTED]\Desktop\mbox.txt");
Scanner inputFile = new Scanner(file);
ArrayList<Integer> ConfidenceLevels = new ArrayList<>();
String [] DSPAM;
String line;
while(inputFile.hasNext())
{
line = inputFile.nextLine();
if(line.startsWith("X-DSPAM-Confidence:"))
{
DSPAM = line.split(" 0");
int x = Integer.parseInt(DSPAM[1].trim());
ConfidenceLevels.add(x);
}
}
System.out.println(ConfidenceLevels);
System.out.println(ConfidenceLevels.size());
}
您只是在向列表中添加元素。
如果你想要它们的总和,你可以创建一个整数变量并添加它。
public static void main(String[] args) throws IOException {
File file = new File("C:\Users\[REDACTED]\Desktop\mbox.txt");
Scanner inputFile = new Scanner(file);
Integer confidenceLevelsSum = 0;
String [] DSPAM;
String line;
while(inputFile.hasNext())
{
line = inputFile.nextLine();
if(line.startsWith("X-DSPAM-Confidence:"))
{
DSPAM = line.split(" 0");
// Check if it's null before trying to convert it.
if (DSPAM[1] == null) {
continue;
}
int x = Integer.parseInt(DSPAM[1].trim());
confidenceLevelsSum += x;
}
}
System.out.println(confidenceLevelsSum);
}
您可以尝试使用 IntSummaryStatistics,您可以获得此列表的 min/max/average/sum。
IntSummaryStatistics intSummaryStatistics = ConfidenceLevels.stream().mapToInt(Integer::intValue).summaryStatistics();
System.out.println(intSummaryStatistics.getAverage());
System.out.println(intSummaryStatistics.getMin());
System.out.println(intSummaryStatistics.getMax());
System.out.println(intSummaryStatistics.getSum());
我正在尝试将 ArrayList 中的所有值相加,但无法得出总和。我必须找到从文本文件中提取的数字的平均值。
public static void main(String[] args) throws IOException {
File file = new File("C:\Users\[REDACTED]\Desktop\mbox.txt");
Scanner inputFile = new Scanner(file);
ArrayList<Integer> ConfidenceLevels = new ArrayList<>();
String [] DSPAM;
String line;
while(inputFile.hasNext())
{
line = inputFile.nextLine();
if(line.startsWith("X-DSPAM-Confidence:"))
{
DSPAM = line.split(" 0");
int x = Integer.parseInt(DSPAM[1].trim());
ConfidenceLevels.add(x);
}
}
System.out.println(ConfidenceLevels);
System.out.println(ConfidenceLevels.size());
}
您只是在向列表中添加元素。 如果你想要它们的总和,你可以创建一个整数变量并添加它。
public static void main(String[] args) throws IOException {
File file = new File("C:\Users\[REDACTED]\Desktop\mbox.txt");
Scanner inputFile = new Scanner(file);
Integer confidenceLevelsSum = 0;
String [] DSPAM;
String line;
while(inputFile.hasNext())
{
line = inputFile.nextLine();
if(line.startsWith("X-DSPAM-Confidence:"))
{
DSPAM = line.split(" 0");
// Check if it's null before trying to convert it.
if (DSPAM[1] == null) {
continue;
}
int x = Integer.parseInt(DSPAM[1].trim());
confidenceLevelsSum += x;
}
}
System.out.println(confidenceLevelsSum);
}
您可以尝试使用 IntSummaryStatistics,您可以获得此列表的 min/max/average/sum。
IntSummaryStatistics intSummaryStatistics = ConfidenceLevels.stream().mapToInt(Integer::intValue).summaryStatistics();
System.out.println(intSummaryStatistics.getAverage());
System.out.println(intSummaryStatistics.getMin());
System.out.println(intSummaryStatistics.getMax());
System.out.println(intSummaryStatistics.getSum());