View difference between Paste ID: M4NW5QuB and J0nRWcPm
SHOW: | | - or go back to the newest paste.
1
package ch.claude_martin;
2
3
import java.util.stream.IntStream;
4
5
public final class SomeClass {
6
  private static final String FORMAT = "%s, %d, %s%n";
7
8
  static final class Student {
9
    private final String name, gender;
10
    private final int age; // note that you should store the birth year instead
11
12
    public Student(String name, int age, String gender) {
13
      this.name = name;
14
      this.age = age;
15-
    for (int i = 0; i < names.length; i++) {
15+
      this.gender = gender;
16-
      System.out.format(FORMAT, names[i], ages[i], gender[i]);
16+
17
18-
    System.out.println("---");
18+
    public int getAge() {
19
      return age;
20-
    IntStream.range(0, names.length).mapToObj(i -> String.format(FORMAT, names[i], ages[i], gender[i]))
20+
21
22
    public String getName() {
23
      return name;
24
    }
25
26
    public String getGender() {
27
      return gender;
28
    }
29
30
    @Override
31
    public String toString() {
32
      return String.format(FORMAT, name, age, gender);
33
    }
34
  }
35
36
  public static void main(String[] args) {
37
    String[] names = { "Alice", "Bob", "Oscar" };
38
    int[] ages = { 35, 65, 23 };
39
    String[] gender = { "Cis Female", "Cis Male", "Other" };
40
41
    assert names.length == ages.length && names.length == gender.length;
42
43
    IntStream.range(0, names.length).mapToObj(i -> new Student(names[i], ages[i], gender[i]))
44
        .forEach(System.out::print);
45
46
  }
47
48
}