-3

I want to add values to the objects with out looping because if there are 1000 of objects then I don't want to loop all of them.I want to add age randomly to the students based on the Name of the student.Is there are any way to add values

Here is the code

import java.util.*;  
import java.io.*; 

class Student{
  Student(String Name){
    this.Name=Name;
  }
  String  Name;
  int age;
}

public class HelloWorld{
 public static void main(String []args){
    String a []={"Ram","Krishna","Sam","Tom"};
    ArrayList<Student> al = new ArrayList<Student>();
    for(int i=0;i<a.length;i++){
        Student c;
        c=new Student(a[i]);
        al.add(c);
    }
    for(Student obj:al){
        if(obj.Name.equals("Krishna")){
            obj.age=24;
        }
        System.out.println("Name = "+ obj.Name + " Age = " + obj.age);  
    }  
 }
}
  • Please show your code and describe your specific problem. Read [How do I ask a good question](https://stackoverflow.com/help/how-to-ask) and [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). – vanje Nov 06 '17 at 09:37
  • See also [How do I compare strings in Java?](https://stackoverflow.com/q/513832/1553851) – shmosel Nov 10 '17 at 06:47
  • I have added the code, now the question is clear I guess please help me to solve this problem @vanje – Vamsi achyuta Nov 10 '17 at 06:50

1 Answers1

0

First some minor points:

You should never use the fields directly but create getter and setters instead. The fields should be private. Variable names should start with a lower case letter by convention. So this would be the adjusted Student class:

public class Student {
  private String name;
  private int age;

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

  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public int getAge() {
    return age;
  }
  public void setAge(int age) {
    this.age = age;
  }
}

To store the Student objects you can use a map with names as keys and Student instances as values.

It is good practice to declare the variable only with the interface type Map and not with the concrete implementation HashMap. A hash map has O(1) complexity for searching by key. So you don't need a loop to iterate through all Student instances. (The HashMap.get() implementation doesn't use a loop internally.)

public static void main(String[] args) {
  String a [] = {"Ram", "Krishna", "Sam", "Tom"};

  // Keys: student names
  Map<String, Student> al = new HashMap<String, Student>();

  // Fill the Student's map
  for(int i = 0; i < a.length; i++){
    String name = a[i];
    al.put(name, new Student(name));
  }

  // Manipulate one student by name. If there is no entry for that name we get null.
  // So we better check for null before using it.
  Student student = al.get("Krishna");
  if(student != null) {
    student.setAge(24);
  }

  // Output the all students
  for(Student obj: al.values()){
    System.out.println("Name = "+ obj.getName() + " Age = " + obj.getAge());  
  }  
}
vanje
  • 10,180
  • 2
  • 31
  • 47