Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I give each person in this list a raise

Tags:

java

Need to use a for-each loop and the raise each person gets is 1000.

Would it be like this?

int raise = 1000;
for (Person i : people){
    people.add(raise, i);       
    }

im working with this

   public ArrayList<Person> people;
like image 988
user695696 Avatar asked Dec 07 '25 10:12

user695696


1 Answers

If what you want is to raise for example salary by 1000 for every person, it would be more like:

1.Define a class.

public class Person {

    private int salary;

    public int getSalary() {
        return salary;
    }

    public void setSalary(int salary) {
        this.salary = salary;
    }

    public void raiseSalary(int salary) {
        this.salary += salary;
    }
}

2.Use raiseSalary(int salary) method on every Person in your for-each loop.

List<Person> people = new ArrayList<Person>();
people.add(new Person());
for (Person person : people) {
    person.raiseSalary(1000);
}

Dont forget to import java.util.* in order to use List and ArrayList.

Read about for-each loop here.

like image 147
JMelnik Avatar answered Dec 10 '25 01:12

JMelnik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!