java - 填充数组列表。爪哇
问题描述
我有一个字符串,我拆分为令牌。我正在尝试用 for 循环填充每个数组列表。我已经很接近了,但是当我测试打印出数组列表时,它没有正确填充。
import java.io.*;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Arrays;
public class VicePresidents {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
String filename = "VicePresidentAges.csv";
File file = new File(filename);
Scanner infile = new Scanner(file);
ArrayList<String> names = new ArrayList<String>();
ArrayList<Integer> ages = new ArrayList<Integer>();
while (infile.hasNext())
{
String line = infile.nextLine();
String[] tokens = line.split(",");
for(int i = 0; i < tokens[0].length(); i++)
{
names.add(tokens[0]);
}
for(int j = 0; j < tokens[1].length(); j++)
{
ages.add(Integer.parseInt(tokens[1]));
}
System.out.println(names);
System.out.println(ages);
//Divide the line into its tokens (There should be 2 tokens per line)
//put the tokens into their correct ArrayList
}
infile.close();
//Go through the ages ArrayList looking for the youngest age.
//Hold onto the *index* where the youngest age is located in the ArrayList.
//Print out the name and age for the youngest Vice President using the index
//you just found from above
}
}
我的输出看起来像这样:[John Adams, John Adams, John Adams, John Adams, John Adams, John Adams]
下面的年龄是这样的:[53, 53, 53, 53, 45, 45, 65, 65, 68, 68, 42, 42, 42, 42, 50, 50]
解决方案
对您的代码进行了一些修改以帮助您入门,尝试理解并改进它(提取方法,使用 java.nio 读取文件,...)
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class VicePresidents {
public static void main(String args[]) throws IOException {
String filename = "VicePresidentAges.csv";
File file = new File(filename);
Scanner infile = new Scanner(file);
ArrayList<String> names = new ArrayList<>();
ArrayList<Integer> ages = new ArrayList<>();
while (infile.hasNext()){
String line = infile.nextLine();
//Divide the line into its tokens (There should be 2 tokens per line)
String[] tokens = line.split(",");
//put the tokens into their correct ArrayList
names.add(tokens[0]);
ages.add(Integer.parseInt(tokens[1]));
}
infile.close();
//print both list just to check
System.out.println(names);
System.out.println(ages);
//Go through the ages ArrayList looking for the youngest age.
//Hold onto the *index* where the youngest age is located in the ArrayList.
int youngestIndex = 0;
for(int i = 0; i < ages.size(); i++){
if(ages.get(i) < ages.get(youngestIndex)){
youngestIndex = i;
}
}
//Print out the name and age for the youngest Vice President using the index
//you just found from above
System.out.println("Youngest Vice President:");
System.out.println("Name: " + names.get(youngestIndex) + " age: " + ages.get(youngestIndex));
}
}
推荐阅读
- spring - 使用 homebrew 在 Mac 上安装 Spring Boot CLI 不会注册命令行完成脚本
- c# - Mongodb:使用数组值更新数组中的元素
- wpf - WPF 在他的填充期间防止 MVVM 中的火组合框源选择
- opengl-es - 尝试在 Expo 中使用多个纹理时出现黑屏
- android - 适用于三星设备的 Android 11 中的问题(Sqlite 问题)
- docker - CI:在 gradle:docker 中找不到 docker
- awk - 重复数据但行数不同的连续平均
- django - 扩展自定义用户模型后,身份验证方法不起作用
- charts - 如何使用 chart.js 在水平条形图上绘制垂直线?
- sql - SQL 表架构