SIGN IN YOUR ACCOUNT TO HAVE ACCESS TO DIFFERENT FEATURES

FORGOT YOUR PASSWORD?

FORGOT YOUR DETAILS?

AAH, WAIT, I REMEMBER NOW!
Openbaraza

Openbaraza

Openbaraza

Baraza is a Swahili word that means a deliberation meeting held by a collective group of people with wisdom.

T : +254 (20) 2243097 / 2227100
Email: info@openbaraza.org

OPENBARAZA
Dew CIS Solutions LTD Barclays Plaza, 12th Floor. Loita Street.

Open in Google Maps
  • Framework
  • Projects
    • Human Resource
      • Human Resource
      • Time Attendance
      • Project Planning
    • Finance Management
      • Business
      • Payroll
    • Investment Management
      • Banking
      • Chama
      • Sacco
    • Case Management
      • Judiciary
    • Real Estate Management
      • Property
    • Academic Management
      • UMIS
      • Schools
  • Forums
    • HCM HR Payroll
    • Business
    • Sacco
    • Banking
  • Blog & News
    • OpenBaraza Framework
    • Databases
    • Java Programming
    • Scrum
  • Contact Us
  • Home
  • Blog & News
  • Java Programming
  • 07. JAVA ARRAYS: Store Student Details Using Arrays and List
July 11, 2025

07. JAVA ARRAYS: Store Student Details Using Arrays and List

07. JAVA ARRAYS: Store Student Details Using Arrays and List

by dennis@dennis.me.ke / Tuesday, 01 October 2019 / Published in Java Programming

– – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

LIVE A LITTLE:

A woman and her husband were having a conversation about her boss…

Woman:My boss wants to sleep with me.

Man:What!! and why? Don’t you dare think about it!

Woman:But he has an offer! He says he will drop 2,500 dollars on the ground and by the time I pick it up, he will be done.

Man: That’s quite a lot of money. We could really use it! Here’s what you’ll do when he drops it, pick it up quickly and tell him you are going home to your husband.

A few hours later…..

Man:Were you able to do it?

Woman:That bastard used coins!!

– – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

 

So in the previous lesson we learned about classes (refer to blog on classes using a JSON string) in Java and we used the student class. In this blog using the same concept, we shall register a group of students using an array and use the help of a list. We shall be able to see the list of the students.

Refer to the blog Class in java

Compile the ArrayStudent class since it contains the main class.

Output:

 

Use the same student class and the class below for list to solve the same problem.

Compile the ListStudent class since it contains the main class.

 

Output:

 

Explanation:

This program requests and takes the name, age and sex of a student, creates a student object parses the object for the attributes and stores them in an array, then retrieves and displays them from the array

// This imports the scanner package to take in user input

import java.util.Scanner;

// here we create class student

public class Student {

//Then we declare variables to be used in the program that represent the student. Public to be accessed from anyway in the program.

Public String name         //student first name

public String age;         //Student Age

public String sex;                    //Student Gender

 

 //Setter and Getter Methods for the Student Class Attributes

public void setName(String name) {

this.name = name;

}

public String getName() {

return name;

}

 

public void setAge(String age) {

this.age = age;

}

public String getAge() {

return age;

}

 

public void setSex(String sex) {

this.sex = sex;

}

public String getSex() {

return sex;

}

 

//Student Class Constructor

public Student() {

Scanner n = new Scanner(System.in);

System.out.print(“First Name: “);

this.setName(n.next());

 

System.out.print(“Age: “);

this.setAge(n.next());

 

System.out.print(“Gender: “);

this.setSex(n.next());

}//End of constructor

}//End of Student class

 

This is the class that contains the main method and it uses arrays.

 

//Start of array class

public class ArrayStudent {

            //main method

            public static void main(String[] args) {

       //Create Array to store Students

                       Student[] studentArray = new Student[3];

 

       //Add Students to Array

                       for (int i=0; i<3; i++) {

Student s = new Student();

studentArray[i] = s;

       }//End of for loop

 

       //Display Students from Array

                       for(int i=0; i<3; i++) {

Student s = studentArray[i];

System.out.println(s.getName()+”, “+s.getAge()+”, “+s.getSex());

}End of the for loop

}End of main method

}End of array class

 

This is the class that contains the main method and it uses list.

 

// This imports the ArrayList package to take in user input

import java.util.ArrayList;

// This imports the List package to take in user input

import java.util.List;

 

//Start of ListStudent class

public class ListStudent {

//Main method

            public static void main(String[] args) {

                       //Create List to store Students

              List<Student> studentList = new ArrayList<Student>();

 

              //Add Students to List

              Student s1 = new Student();

studentList.add(s1);

 

Student s2 = new Student();

studentList.add(s2);

 

Student s3 = new Student();

studentList.add(s3);

 

         //Display Students from List

              for(int i=0; i<3; i++) {

Student s = studentList.get(i);

System.out.println(s.getName()+”, “+s.getAge()+”, “+s.getSex());

 

}//End of for loop

}//End of main method

}//End of list class

12
  • Tweet

About dennis@dennis.me.ke

What you can read next

03. Operators
08. BODMAS IN JAVA
05. Variables

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • 08. BODMAS IN JAVA

  • 06. Classes: Store Student Details Using Json Format

  • 02. SPRINT RETROSPECTIVE

  • 01. SCRUM (DEMO)

  • 05. Variables

Categories

  • Databases
  • Java Programming
  • OpenBaraza Framework
  • Scrum

QUICK LINKS

  • Home
  • Our Stories
  • Contact Us
  • Terms and Conditions
  • Our Policy

VISIT US

Open Baraza
Barclays Plaza, 12th Floor.
Loita Street, Nairobi.

Open in Google Maps

GET IN TOUCH

Tel: +254 (20) 2243097 / 2227100
     +254(726)209214, +254(738)819505
Email: info@openbaraza.org

Follow Us

Could not authenticate you.
  • GET SOCIAL
Openbaraza

© All rights reserved | Open Baraza developed by DewCis Solutions.

TOP