我一直在寻找解决问题的方法而没有任何运气。所以现在我在这里寻求帮助。
我正在通过以下课程创建“群组”:
public class Group {
private String groupID;
private ArrayList<User> usersInGroup;
User类看起来像这样: 注意:我已经有一个包含所有现有用户的ArrayList。
public class User {
private String firstName;
private String lastName;
private String age;
private String gender;
private String usernameID;
private String password;
我已经在“groupData.txt”CSV文本文件中添加了groupID字段,如下所示:
public static ArrayList<Group> listOfCreatedGroups() throws IOException {
ArrayList<Group> listOfGroups = new ArrayList<>();
FileReader fr = new FileReader("src/groupData.txt");
BufferedReader bfr = new BufferedReader(fr);
String line;
int totalLine = Destination.linesInFile("src/groupData.txt"); //total lines in file
for (int i = 0; i < totalLine; i++) {
line = bfr.readLine();
String[] groupID = line.split(",");
Group temp = new Group();
temp.setGroupID(groupID[0]);
listOfGroups.add(temp);
}
try {
bfr.close();
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
return listOfGroups;
}
“groupData.txt”文件的结构如下:
行示例= groupID,String_1,String2,String3 ... Stringn,\ n
GROUPID,用户,用户,用户,用户,用户,用户,用户
GROUPID,用户,用户,用户
GROUPID,用户,用户,用户,用户,用户
GROUPID,用户,用户
GROUPID,用户,用户,用户,用户
由于我只将每个组 User.usernameID 中的用户数作为文本文件中的1到n个字符串,因此我无法将整个User对象添加到Arraylist usersInGroup。 我不知何故需要隔离usernameID并找到相应的Users并将它们添加到ArrayList usersInGroup。
我希望你们中的任何一个人都能给我一个正确方向的暗示。感谢。
答案 0 :(得分:0)
我不知道这是不是你想要的,因为我没有找到非常具体的用户数据,甚至没有提到你想要的用户数据。但请告诉我这是否足够
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
public class Group {
private static String groupID;
private static ArrayList<User> usersInGroup = new ArrayList<User>();
public static void main (String [] args) throws IOException {
addListToGroup(readFile());
}
public static void addListToGroup(ArrayList<ArrayList<String>> list) {
for (int i = 0; i < list.size(); i++) {
groupID = list.get(i).get(0);
for (int x = 0; x < list.get(i).size(); x++) {
User temp = new User(); // change this to however you setup the txt file
// the information from the list is in list.get(i).get(x) in order as in the textfile
temp.setAge(null);
temp.setFirstName(null);
temp.setGender(null);
temp.setLastName(null);
temp.setPassword(null);
temp.setUsernameID(null);
usersInGroup.add(temp);
}
}
}
public static ArrayList<ArrayList<String>> readFile() throws IOException {
List<String> temp = new ArrayList<String>();
Path path = Paths.get("file.txt");
temp = Files.readAllLines(path);
ArrayList<ArrayList<String>> lines = new ArrayList<ArrayList<String>>();
for (int i = 0; i < temp.size(); i++) {
String [] s = temp.get(i).split(",");
ArrayList<String> quickArray = new ArrayList<String>();
for (int x=0; x < s.length; x++) {
quickArray.add(s[x]);
}
lines.add(quickArray);
}
return lines;
}
}
class User {
private String firstName;
private String lastName;
private String age;
private String gender;
private String usernameID;
private String password;
//setters and getters
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getUsernameID() {
return usernameID;
}
public void setUsernameID(String usernameID) {
this.usernameID = usernameID;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}