如何在Java中从自定义数组列表中获取某个字符串?

c9x0cxw0  于 2023-01-16  发布在  Java
关注(0)|答案(2)|浏览(114)

我想读取一个文件的内容,并将文件的每一行存储到一个自定义的ArrayList中。文件的内容如下所示:
这是我目前所做的:

public static ArrayList<st> myArray = new ArrayList<st>();
public static st s = new st();

s.id=lineScan.next();
s.name=lineScan.next();
s.gender=lineScan.next();
s.age=lineScan.nextInt();
myArray.add(s);

class st {
    String id;
    String name; 
    String gender; 
    int age; 
}

我想获取www.example.com字符串,每次性别是男性时,把它们一起打印出来,对女性也是如此,但我不知道如何从数组列表中获取某些字符串.s.name string for every time the gender is male and print them out together, and do the same with the female gender, but I don't know how to get certain strings from the array list.

0kjbasz6

0kjbasz61#

以下是abacus-common的代码

Stream.of(IOUtil.readLines(new File(filePath)))
    .map(line -> Splitter.with(" ").trim(true).splitToArray(line))
    .groupBy(a -> a[1], a -> a[0], (a, b) -> a + ", " + b)
    .forEach(N::println);
// output:
// female=Julia, Mia
// male=Marwan, John, William

声明:我是算盘的开发者。

jfgube3f

jfgube3f2#

我会这样做

for(st s: myArray){
         System.out.println(s.gender+" "+s.name)}

相关问题