如何将数组列表中的一个元素替换为另一个元素?

huangapple go评论71阅读模式
英文:

How to I replace an element in a array list with another?

问题

0

以下是翻译好的部分:

我有一个任务,要求计算下面代码中的所有内容。所有工作正常 - 我只需要计算超过160小时的任何月工时,按正常小时费率的1.5倍支付。我的数学似乎是正确的,并且计算正常:

((hours - 160) * overtime) + (160 * hourlyRate)

但我不知道是否应该将这个if语句放在正确的方法中,或者它是否应该是一个if语句。在此之前,我的增加/减少支付方法是有效的,它们需要保留。我删除了一些内容,以便阅读更容易。

HourlyWorker类:

public class HourlyWorker extends Employee
{
private int hours;
private double hourlyRate;
private double monthlyPay;
private double overtime = (1.5 * hourlyRate);

public HourlyWorker(String last, String first, String ID, double rate)
{
   super(last, first, ID);
   hourlyRate = rate;
}

public void setHours(int hours)
{
   this.hours = hours;
}

public int getHours()
{
   return hours;
}

public void setHourlyRate(double rate)
{
   this.hourlyRate = rate;
}

public double getHourlyRate()
{
   return hourlyRate;
}


public double getMonthlyPay()
{
   if (hours > 160)
   {
      monthlyPay = ((hours - 160) * overtime) + (160 * hourlyRate);
   }
   else 
   {
      monthlyPay = hourlyRate * hours;
   }
   return monthlyPay;
}

public void increasePay(double percentage)
{
   hourlyRate *= 1 + percentage / 100;
}

public void decreasePay(double percentage)
{
   hourlyRate *= 1 - percentage / 100;
}

}
我正在进行测试的内容:

public class TestEmployee2
{
   public static void main(String[] args)
   {
   Employee [] staff = new Employee[3];
      HourlyWorker hw1 = new HourlyWorker("Bee", "Busy", "BB1265", 10);
       
      hw1.setHours(200);    
      staff[0] = hw1;

   System.out.println(staff[0].getMonthlyPay());
   staff[0].increasePay(10);
   System.out.println(staff[0].getMonthlyPay());
}
}

输出结果为:
1600(初始月薪率,包括40小时的加班工时和160小时的常规工时)
1760(月薪支付增加10%)

应该是:
2006
2206.6
英文:

0

I have an assignment which asks for everything I have in the code below. That all works fine - I just need to calculate any monthly hours over 160 hours to be paid at 1.5 times the normal hourly rate. My math seems sound and calculates fine:

((hours - 160) * overtime) + (160 * hourlyRate)

But I dont know if I'm putting this if statement in the right method or if it even should be an if statement. My increase/decreasePay methods are working prior to this and they need to stay. I removed some things so it's easier to read.

HourlyWorker Class:

public class HourlyWorker extends Employee
{
private int hours;
private double hourlyRate;
private double monthlyPay;
private double overtime = (1.5 * hourlyRate);

public HourlyWorker(String last, String first, String ID, double rate)
{
   super(last, first, ID);
   hourlyRate = rate;
}

public void setHours(int hours)
{
   this.hours = hours;
}

public int getHours()
{
   return hours;
}

public void setHourlyRate(double rate)
{
   this.hourlyRate = rate;
}

public double getHourlyRate()
{
   return hourlyRate;
}


public double getMonthlyPay()
{
   if (hours > 160)
   {
      monthlyPay = ((hours - 160) * overtime) + (160 * hourlyRate);
   }
   else 
   {
      monthlyPay = hourlyRate * hours;
   }
   return monthlyPay;
}

public void increasePay(double percentage)
{
   hourlyRate *= 1 + percentage / 100;
}

public void decreasePay(double percentage)
{
   hourlyRate *= 1 - percentage / 100;
}

}
What I'm testing with:

public class TestEmployee2
{
   public static void main(String[] args)
   {
   Employee [] staff = new Employee[3];
      HourlyWorker hw1 = new HourlyWorker("Bee", "Busy", "BB1265", 10);
       
      hw1.setHours(200);    
      staff[0] = hw1;

   System.out.println(staff[0].getMonthlyPay());
   staff[0].increasePay(10);
   System.out.println(staff[0].getMonthlyPay());
}
}

Output is:
1600 (initial monthly rate, with 40 overtime hours and 160 regular hours)
1760 (10% increase to the monthlyPay)

Should be:
2006
2206.6

答案1

得分: 1

你离目标很近了。对于set()方法,你需要将位置作为第一个参数添加,即public E set(int index, E element)

this.artists.set(0, new Artist(artist, genre));    // 这应该可以工作
英文:

You're almost there. Fort set() method, you have to add position as first argument public E set(int index, E element):

this.artists.set(0, new Artist(artist, genre));    // this should work

答案2

得分: 0

更新ArrayList中的对象,请使用set方法。

链接:https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html#set-int-E-

该方法接受两个参数,即index和新对象。

要获取index,请参阅链接:https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html#indexOf-java.lang.Object-

英文:

To update an Object in an ArrayList use the set method

https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html#set-int-E-

This takes two arguments, the index and the new Object.

To get the index see https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html#indexOf-java.lang.Object-

答案3

得分: 0

假设您已将以下艺术家添加到您的列表中:

new Artist("Rolling Stones", "Rock");
new Artist("Abba", "Rock");

现在,如果您希望您的addArtists方法在艺术家再次被添加时实际上更新艺术家的流派,您可以这样做:

我还假设this.artists是一个Collection

public void addArtist(String artistName, String genre) {
    this.artists.removeIf((artist) -> artist.name.equals(artistName));
    this.artists.add(new Artist(artistName, genre));
}

该方法首先使用removeIf从列表中移除具有相同姓名的任何艺术家,然后添加新的艺术家。


现在,当您执行以下操作时:

artists.addArtist("Rolling Stones", "Rock");
artists.addArtist("Abba", "Pop");

之前添加的艺术家将被这些新的艺术家所替代。

英文:

Assuming you have added the following artists to your list:

new Artist("Rolling Stones", "Rock");
new Artist("Abba", "Rock");  

Now you want your addArtists method to actually update an artist's genre if they were added again, you can do it like this:

(I'm also assuming this.artists is a Collection.)

public void addArtist(String artistName, String genre) {
    this.artists.removeIf((artist) -> artist.name.equals(artistName)));
    this.artists.add(new Artist(artistName, genre));
}

The method first removes (using removeIf) any artist with that name from the list, then it adds the new artist.


Now when you do:

artists.addArtist("Rolling Stones", "Rock");
artists.addArtist("Abba", "Pop");

The previous artists you had added will be replaced by these

huangapple
  • 本文由 发表于 2020年10月8日 12:41:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/64255906.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定