how to access single element in array for example how can i get mark age

This is not an Array, but a List (albeit an ArrayList being backed by an array internally, but that’s implementation detail and slightly beside the point here).

When you add a Student to your list, it becomes an element of that list.
You can access List elements using list.get(int index).

“Mark” or 23 in your example are not elements of the list, but properties of the Object “Student” (remember that in Java, class names are CamelCase and usually singular by convention)

Therefore, what I think you want is something like:

Student mark = new Student("Mark", "Markos", 23, 32);
//assuming a constructor exists e.g.:
//Student(String firstname, String lastname, int age, int studentNumber)

here.add(mark);
Student stillMark = here.get(0);   //assuming no other modification
String marksName = mark.getName(); //"Mark", assuming this getter exists
int marksAge = mark.getAge();      //23, ditto

mark.setAge(24); //who's birthday is it?
System.out.println(stillMark.getAge()); //24 - mark and stillMark point to exactly the same object.

These are basic Java (and other programming language) concepts. Good luck with your learning.

Edit: Calculate the average age on Java 8+ (also see Calculating average of an array list?)

OptionalDouble avgAge = list
     .stream()               //Use Java 8 streams to iterate
     .mapToInt(Student::age) //transform into an IntStream
     .average();             //IntStream has this beautiful method
if(avgAge.isPresent()) {
    //this will be false if the list was empty.
}

Leave a Comment