0
public class pro1{
    static ArrayList <String> student = new ArrayList<String>();
    static ArrayList <Integer> id = new ArrayList<Integer>();
    String name;
    int ID;
    public pro1() {
        this.name = "";
        this.ID = 0;
    }
    public pro1(String name, int ID) {
        this.name = name;
        this.ID = ID;
    }
    public boolean addStudent(String name, int ID) {
        student.add(name);
        id.add(ID);
        return true;
    }
    /*@Override
    public String toString() {
        return name + ID;
    }*/
    public static void main(String args[]) {
        pro1 stu = new pro1();
        stu.addStudent("john", 1);
        stu.addStudent("johnny", 2);
        System.out.println(stu);
    }

}

I want to print out both the name of the student and the student id using ArrayList. however, I'm not sure how to do that since in this class I can only print out the ArrayList of names or the ArrayList of id. I'm thinking of maybe using another class to create a student object, but I'm not sure how to do so.

  • This may be helpful: https://stackoverflow.com/questions/19602601/create-an-arraylist-with-multiple-object-types – Stidgeon Feb 10 '20 at 01:02
  • Separate the student object from the List of Students. You should have a Student Class that contains the `id` and the `name` and then you can have a List of Students. – Scary Wombat Feb 10 '20 at 01:05

3 Answers3

1

great question, I think the best solution for this would be to create a Student object just like you thought!

    public static class Student {

        private final String name;

        private final int id;

        public Student(String name, int id) {
            this.name = name;
            this.id = id;
        }

        @Override
        public String toString() {
            return String.format("name=%s, id=%s", name, id);
        }
    }

    public static class School {

        private final List<Student> students;

        public School(List<Student> students) {
            this.students = students;
        }

        public void add(Student student) {
            students.add(student);
        }

        public List<Student> getStudents() {
            return students;
        }
    }

    public static void main(String... args) {
        School school = new School(new ArrayList<>());

        school.add(new Student("jason", 1));
        school.add(new Student("jonny", 2));

        school.getStudents().forEach(System.out::println);
    }
Jason
  • 5,154
  • 2
  • 12
  • 22
0

Non-OOP

Loop the pair of lists.

First sanity-check: Are the two lists the same size?

if( students.size() != ids.size() ) { … Houston, we have a problem }

Then loop. Use one index number to pull from both lists.

for( int index = 0 ; 
     index < students.size() ; 
     index ++ 
) 
{
    System.out.println( 
        students.get( index ) + 
        " | " + 
        ids.get( index ) 
    ) ;
}

OOP

The object-oriented approach would be to define a class. The class would have two member fields, the name and the id of the particular student.

Then create a method that outputs a String with your desired output.

All this has been covered many times on Stack Overflow. So search to learn more. For example: Creating simple Student class and How to override toString() properly in Java?

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
-1

Uncomment and modify your toString() method as below. It will print both student and id.

@Override
public String toString() {
    return student.toString() + id.toString();
}

If you want to have Student to Id mapping, best is to have them in Map collection. Id as key and student name as value.

kann
  • 687
  • 10
  • 22