Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7824235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T08:31:51+00:00 2026-06-02T08:31:51+00:00

I have created a ArrayList<Student> in a object StudentList list1 that is saving[Serialization] a

  • 0

I have created a ArrayList<Student> in a object StudentList list1 that is saving[Serialization] a student’s information (name,id,age,gpa,etc) into the list1, so that the list1[0] = 1st student's info, then the list1[1] = 2nd student's info and so on.

Also a new ArrayList<Subject> in a object SubjectList list2 for all subject of a student at index 0 example list2[0]=(java,math,etc) [for first student] list2[1]=(c++,english,etc) [for second student] saved in a file.

I want to add the subject next to the student info:

list1 index[0]=1st Student info, index[1]=1st Student’s Subjects.
index[2]=1st Student info,index[3]=1st Student’s Subjects.

I am stuck with this simple problem.Help please.

package studentPanel;

public class Main {
    public static void main(String[] args){     
        Student s = new Student(null, null, null, null);
        s.stulist.add(new Student("Smith", "1", "M", "3"));
        s.stulist.add(new Student("Jenny", "2", "F", "4"));
        s.stulist.add(new Student("Roger", "3", "M", "2"));     
        System.out.println(""+s.stulist);
        for(int i=0;i<s.stulist.size();i++){
            Student search = s.stulist.get(i);
            if(search.toString().contains("Jenny")){
                System.out.println("Found"+i);
                s.addSubject(s.new Subject("OOP","007"));
                s.addSubjects(s.sublist);
                System.out.println(""+s.stulist.get(i)+""+s.sublist);
            }
            else System.out.println("Not Found"+i);
        }
                System.out.println(""+s.stulist);
    }
}

package studentPanel;

import java.util.*;

public class Student {
    public String name, id, gender, cgpa;

    ArrayList<Subject> sublist = new ArrayList<Subject>();
    ArrayList<Student> stulist = new ArrayList<Student>();  
    public void addSubject(Subject new_subject) {
        sublist.add(new_subject);
    }
    public void addSubjects(List<Subject> subjects_list) {
        for (Subject s : subjects_list)
            addSubject(s);
    }   
    public Student(String name, String id, String gender, String cgpa) {
        this.name = name;
        this.id = id;
        this.gender = gender;
        this.cgpa = cgpa;
    }
    public String toString() {
        return "Name: " + name + "\tID: " + id + "\tGender: " + gender + "\tCGPA: " + 

cgpa+ "\n";
    }
    public class Subject {
        public String cname,cid;
        public Subject(String cname, String cid) {
            this.cname = cname;
            this.cid = cid;
        }
        public String toString() {
            return "Course: " + cname + "\tCode: " + cid;
        }
    }
}

I just can’t get it right, it was suppose to add the subject `s.addSubject(s.new Subject

(“OOP”,”007″));` to the student Jenny.

Output Should be like:

[Name: Smith ID: 1 Gender: M CGPA: 3

, Name: Jenny ID: 2 Gender: F CGPA: 4

, Name: Roger ID: 3 Gender: M CGPA: 2 ]

Not Found0

Found1

[ Name: Jenny ID: 2 Gender: F CGPA: 4 ][Course: OOP,Code: 007]

Not Found2

[Name: Smith ID: 1 Gender: M CGPA: 3

, Name: Jenny ID: 2 Gender: F CGPA: 4 [Course: OOP,Code: 007]

, Name: Roger ID: 3 Gender: M CGPA: 2 ]

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T08:31:52+00:00Added an answer on June 2, 2026 at 8:31 am

    I think the approach you should be taking is something akin to this :

    class Student{ 
       private String name, ID;
       private int gpa, age;
       //More personalia stuff...
    
       private ArrayList<Subject> subjects;
    
       public void addSubject (Subject new_subject) { subjects.add(new_subject); }
       public void addSubjects (List <Subject> subjects_list) {
           for (Subject s : subjects_list) addSubject(s);
       }
    
       //Getters 
       public int getGPA() { return gpa; }
       public int getAge() { return age; }
       public String getName() { return name; }
    
       //.. More personalia getters
    
       //Setters 
       public void setGPA(int new_gpa) { gpa = new_gpa; }
    
    
    
     }
    
    class Subject { 
        String description;
        String ID;
    
    }
    

    Now if you have a list of students say :

     ArrayList <Student> all_students;
    

    Here you can go through the list and add subjects to any or all of them.

    Edit : added some getters and setters that makes it clearer how you access object info.


    Edit 2 to address your updated post :
    There are a number of issues with your code. Let’s go through them from the most high-level stuff to low (not the same as importance mind you)

    1) Class names
    A class should not be called “Main”. It is unclear what the class is. Instead, call it what it is : a student-subject-registry system. class StudentRegister should be just fine.

    2) Class contents
    You included the class Subject as an inner class in Student. This does not make sense, as a subject is not a part of a student (in literal terms in real life). This is not the worst part of it though; having Subject as an inner class will make you create loads of identical subjects for each and every student. This is completely redundant.

    Now you also included a list of all students in every Student object. Is this intentional? If so, it seems a bit misguided. Student A does not need to, and probably will never know about all other students. Not to mention, everytime you add a new student N, you’ll need to add N to all student objects that currently exist. That is redundant as well.


    A proposed solution

    Let’s think about what you know as a student. You know :

    • your age (private int age)
    • your GPA (private int GPA)
    • your ID (private String ID)
    • your name (private String name)
    • the list of subjects you take (private List subjects)

    Note that you as a student have no control over what the course description and ID is

    All those private variables are private because those are things that you as a student disclose on request of someone. It is akin to someone asking you “What’s your name?” or “What’s your age?” etc. This is what you accomplish by implementing getters like getName() , getAge().

    Now let’s consider the part in italic. When someone asks you what courses you are taking, you simply refer to the list of courses that are common for you and other students that are enrolled at the universities. You do not create your own courses that nobody else knows about. Therefore, a Subject should not be an inner class in your Student class. It is instead a referance (i.e a pointer) to a course object that is offered by the university (continue reading for further explanation)

    Now consider a course object. That course object does not need to know about :

    • the list of students that are enrolled in a given year
    • any other subjects

    Any given course object needs to know :

    • it’s course description
    • course ID
    • a string representation (i.e what you’ve implemented as toString() )

    Finally, in your StudentRegistry class, you should have two lists now :
    – a list of students enrolled at the university : List <Student> all_students;
    – a list of all subjects/courses offered : `List all_subjects;

    Remember that each students have methods for adding subjects? You simply refer to the course objects that exist in the studentRegistry and ask each student to add those to their subject list. No new subject objects will be created. Any and all subjects in every student’s study plan will refer to objects in the all_subjects list contained in the StudentRegistry class

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created an arraylist that is made up of custom objects. Basically the
I have created a JSON object like this: static ArrayList<item> list = new ArrayList<item>();
I have a structure in C#.net. I have created an object of that structure.
I have created a mini installer in NSIS that installs patches for my application.
I have the following ArrayList in my Action Class : List<Student> students = ArrayList<Students>();
i have created synchronized arrayList like this import java.text.SimpleDateFormat; import java.util.*; class HelloThread {
I have a object with 3 variable (id(string), year(int), pay(double)) I have created an
What I need is simple: If oncreate I have an arraylist created with some
I have created two classes, class A that uses a Linked List and class
I have created an ArrayList in my Application class : package a.b.layout; import java.util.ArrayList;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.