将 Array 的元素加载到集合中
Loading elements of Array into a collection
我有一个名字的文本文件(姓氏和名字)。我已经成功地能够使用 RandomAccessFile class 将所有名称加载到字符串数组中。剩下我要做的是,将列表中的每个名字分配给一个名字数组,将列表中的每个姓氏分配给一个姓氏数组。这是我所做的,但我没有得到任何想要的结果。
public static void main(String[] args) {
String fname = "src\workshop7\customers.txt";
String s;
String[] Name;
String[] lastName, firstName;
String last, first;
RandomAccessFile f;
try {
f = new RandomAccessFile(fname, "r");
while ((s = f.readLine()) != null) {
Name = s.split("\s");
System.out.println(Arrays.toString(Name));
for (int i = 0; i < Name.length; i++) {
first = Name[0];
last = Name[1];
System.out.println("last Name: " + last + "First Name: "+ first);
}
}
f.close();
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
请帮帮我,我似乎对使用哪种集合以及如何处理感到困惑谢谢
您可以创建一种方法来读取文件并将数据放入数组中,但是,如果您决定使用数组,则必须以固定大小 b/c 数组创建它在 java
中是不可变的
public class tmp {
public static void main(String[] args) throws FileNotFoundException {
//problem you have to create an array of fixed size
String[] array = new String[4];
readLines(array);
}
public static String[] readLines(String[] lines) throws FileNotFoundException {
//this counter can be printed to check the size of your array
int count = 0; // number of array elements with data
// Create a File class object linked to the name of the file to read
java.io.File myFile = new java.io.File("path/to/file.txt");
// Create a Scanner named infile to read the input stream from the file
Scanner infile = new Scanner(myFile);
/* This while loop reads lines of text into an array. it uses a Scanner class
* boolean function hasNextLine() to see if there another line in the file.
*/
while (infile.hasNextLine()) {
// read a line and put it in an array element
lines[count] = infile.nextLine();
count++; // increment the number of array elements with data
} // end while
infile.close();
return lines;
}
}
但是,首选方法是使用 ArrayList
,这是一个在添加数据时使用动态调整数组大小的对象。换句话说,您无需担心拥有不同大小的文本文件。
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new FileReader("path/of/file.txt"));
String str;
ArrayList<String> list = new ArrayList<String>();
while ((str = in.readLine()) != null) {
list.add(str);
}
String[] stringArr = list.toArray(new String[0]);
关于随机访问的一些知识。
另一方面,类 与 BufferedReader and FileInputStream use a sequential process of reading or writing data. RandomAccess 类似,正如其名称所暗示的那样,它允许对文件内容进行非顺序、随机访问。但是,随机访问通常用于其他应用程序,例如读取和写入 zip 文件。除非您担心速度问题,否则我建议您使用其他 类.
public static void main(String[] args) throws FileNotFoundException {
BufferedReader in = new BufferedReader(new FileReader("src\workshop7\customers.txt"));
String str;
String names[];
List<String> firstName = new ArrayList();
List<String> lastName = new ArrayList();
try {
while ((str = in.readLine()) != null) {
names = str.split("\s");
int count = 0;
do{
firstName.add(names[count]);
lastName.add(names[count+1]);
count = count + 2;
}while(count < names.length);
}
} catch (IOException e) {
e.printStackTrace();
}
// do whatever with firstName list here
System.out.println(firstName);
// do whatever with LastName list here
System.out.println(lastName);
}
我有一个名字的文本文件(姓氏和名字)。我已经成功地能够使用 RandomAccessFile class 将所有名称加载到字符串数组中。剩下我要做的是,将列表中的每个名字分配给一个名字数组,将列表中的每个姓氏分配给一个姓氏数组。这是我所做的,但我没有得到任何想要的结果。
public static void main(String[] args) {
String fname = "src\workshop7\customers.txt";
String s;
String[] Name;
String[] lastName, firstName;
String last, first;
RandomAccessFile f;
try {
f = new RandomAccessFile(fname, "r");
while ((s = f.readLine()) != null) {
Name = s.split("\s");
System.out.println(Arrays.toString(Name));
for (int i = 0; i < Name.length; i++) {
first = Name[0];
last = Name[1];
System.out.println("last Name: " + last + "First Name: "+ first);
}
}
f.close();
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
请帮帮我,我似乎对使用哪种集合以及如何处理感到困惑谢谢
您可以创建一种方法来读取文件并将数据放入数组中,但是,如果您决定使用数组,则必须以固定大小 b/c 数组创建它在 java
中是不可变的 public class tmp {
public static void main(String[] args) throws FileNotFoundException {
//problem you have to create an array of fixed size
String[] array = new String[4];
readLines(array);
}
public static String[] readLines(String[] lines) throws FileNotFoundException {
//this counter can be printed to check the size of your array
int count = 0; // number of array elements with data
// Create a File class object linked to the name of the file to read
java.io.File myFile = new java.io.File("path/to/file.txt");
// Create a Scanner named infile to read the input stream from the file
Scanner infile = new Scanner(myFile);
/* This while loop reads lines of text into an array. it uses a Scanner class
* boolean function hasNextLine() to see if there another line in the file.
*/
while (infile.hasNextLine()) {
// read a line and put it in an array element
lines[count] = infile.nextLine();
count++; // increment the number of array elements with data
} // end while
infile.close();
return lines;
}
}
但是,首选方法是使用 ArrayList
,这是一个在添加数据时使用动态调整数组大小的对象。换句话说,您无需担心拥有不同大小的文本文件。
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new FileReader("path/of/file.txt"));
String str;
ArrayList<String> list = new ArrayList<String>();
while ((str = in.readLine()) != null) {
list.add(str);
}
String[] stringArr = list.toArray(new String[0]);
关于随机访问的一些知识。
另一方面,类 与 BufferedReader and FileInputStream use a sequential process of reading or writing data. RandomAccess 类似,正如其名称所暗示的那样,它允许对文件内容进行非顺序、随机访问。但是,随机访问通常用于其他应用程序,例如读取和写入 zip 文件。除非您担心速度问题,否则我建议您使用其他 类.
public static void main(String[] args) throws FileNotFoundException {
BufferedReader in = new BufferedReader(new FileReader("src\workshop7\customers.txt"));
String str;
String names[];
List<String> firstName = new ArrayList();
List<String> lastName = new ArrayList();
try {
while ((str = in.readLine()) != null) {
names = str.split("\s");
int count = 0;
do{
firstName.add(names[count]);
lastName.add(names[count+1]);
count = count + 2;
}while(count < names.length);
}
} catch (IOException e) {
e.printStackTrace();
}
// do whatever with firstName list here
System.out.println(firstName);
// do whatever with LastName list here
System.out.println(lastName);
}