arraylist<string>..tostring()后如何再转为arraylist
时间: 2023-08-22 17:08:38 浏览: 38
要将一个字符串转换回 ArrayList,可以使用字符串中的元素分隔符将其分割为单个元素,然后将这些元素添加到一个新的 ArrayList 中。以下是一个示例代码:
```java
String str = "element1;element2;element3";
String[] strArray = str.split(";");
ArrayList<String> list = new ArrayList<String>(Arrays.asList(strArray));
```
在这个示例中,我们首先将字符串 `str` 使用分号 `;` 进行分割,得到一个字符串数组 `strArray`,然后将该数组转换为 ArrayList。这个过程使用了 `Arrays.asList()` 方法将数组转换为 List,然后通过 ArrayList 构造函数将其转换为 ArrayList。
相关问题
注释下面每一行代码:public class Shift { private ArrayList<String> kwicList = new ArrayList<String>(); private ArrayList<String> lineTxt; public Shift( ArrayList<String> lineTxt) { this.lineTxt = lineTxt; } public ArrayList<String> getKwicList() { return kwicList; } public ArrayList<String> getLineTxt() { return lineTxt; } public void shift() { //获取每个单词,存入tokens Iterator<String> it = lineTxt.iterator(); while (it.hasNext()) { StringTokenizer token = new StringTokenizer(it.next()); ArrayList<String> tokens = new ArrayList<String>(); int i = 0; //循环添加单词 int count = token.countTokens(); while (i < count) { tokens.add(token.nextToken()); i++; } //display(tokens); //切割各个单词,不断改变起始值和利用loop实现位移。 for (i = 0; i < count; i++) { StringBuffer lineBuffer = new StringBuffer(); int index = i; for (int f = 0; f < count; f++) { //从头继续位移 if (index >= count) index = 0; //存入StringBuffer lineBuffer.append(tokens.get(index)); lineBuffer.append(" "); index++; } String tmp = lineBuffer.toString(); kwicList.add(tmp); } } } }
这段代码定义了一个名为 Shift 的类,其中包含了三个字段和三个方法:
- 字段 kwicList:类型为 ArrayList<String>,存储了所有的关键词索引环(Key Word in Context List)。
- 字段 lineTxt:类型为 ArrayList<String>,存储了所有待处理的文本行。
- 构造方法 Shift():接受一个类型为 ArrayList<String> 的 lineTxt 参数,并将其赋值给 lineTxt 字段。
- 方法 getKwicList():返回 kwicList 字段。
- 方法 getLineTxt():返回 lineTxt 字段。
- 方法 shift():实现关键词索引环的生成过程。
具体来说,shift() 方法中首先通过 Iterator 遍历 lineTxt 中的每一行文本(每一行都是由多个单词组成的字符串),并将这些单词分割出来,存入 tokens 的 ArrayList 中。
然后,对于每个 tokens,利用双重 for 循环遍历每一个单词,不断改变起始值和利用 loop 实现位移。在位移过程中,使用 StringBuffer 存储每个位移后的单词序列,最终将其存入 kwicList 中。这样,kwicList 中就存储了所有的关键词索引环。
Task 2. Create a list of students by reading from student_hobbies.txt and display the list. // - You will also need to create a Student class, which has at least // two data fields: student name and an ArrayList of hobbies (String-type).优化以下代码private static ArrayList<Student> readFile(String string) throws IOException{ ArrayList<Student> studentlist=new ArrayList<Student>(); File file = new File("student_hobbies_JU.txt"); Scanner scanner = new Scanner(file); while (scanner.hasNextLine()) { String line = scanner.nextLine(); String[] data = line.split(", "); System.out.println(data); String name = data[0]; ArrayList<String> hobbies = new ArrayList<String>(); for(int i=0;i<data.length;i++){ hobbies.add(data[i]); } Student student = new Student(name, hobbies); studentlist.add(student); System.out.println(student); } scanner.close(); return studentlist; }//end readFile
下面是优化后的代码,其中将文件名作为参数传入函数中,使函数更加通用:
```
private static ArrayList<Student> readFile(String fileName) throws IOException {
ArrayList<Student> studentList = new ArrayList<Student>();
File file = new File(fileName);
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
String[] data = line.split(", ");
String name = data[0];
ArrayList<String> hobbies = new ArrayList<String>();
for (int i = 1; i < data.length; i++) { // 从第二个元素开始,将所有的兴趣爱好添加到hobbies列表中
hobbies.add(data[i]);
}
Student student = new Student(name, hobbies);
studentList.add(student);
}
scanner.close();
return studentList;
}
```
该函数将指定文件中的内容读取到一个ArrayList<Student>中,并返回该列表。下面是Student类的代码:
```
public class Student {
private String name;
private ArrayList<String> hobbies;
public Student(String name, ArrayList<String> hobbies) {
this.name = name;
this.hobbies = hobbies;
}
public String getName() {
return name;
}
public ArrayList<String> getHobbies() {
return hobbies;
}
public void setName(String name) {
this.name = name;
}
public void setHobbies(ArrayList<String> hobbies) {
this.hobbies = hobbies;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", hobbies=" + hobbies +
'}';
}
}
```
该类包含两个数据字段:name和hobbies,分别表示学生的姓名和兴趣爱好列表。该类还包含构造函数、getter和setter方法以及toString方法,用于打印学生对象的信息。可以使用以下代码调用readFile函数并打印学生列表:
```
try {
ArrayList<Student> studentList = readFile("student_hobbies.txt");
for (Student student : studentList) {
System.out.println(student);
}
} catch (IOException e) {
e.printStackTrace();
}
```