简体   繁体   中英

Change value of an object in a arraylist

I found that this worked :)

I found a way to make it work. Instead of this:

for(int i=0; i<alla.size(); i++){
    if(alla.get(i).getClass().getName().equals("Aktie")){
        alla.get(i).setKurs(0.0);
    }        
}

I got this to work:

for(Värdesak v : alla){
    if(v instanceof Aktie){
        ((Aktie)v).setKurs(0.0);
    }
}

I can't to figure out whats wrong with this one.. I have an arraylist of different objects, and I have a button in my program that is called "stock market crash" which should set all existing stocks course(have no clue what word i'm looking for here) in my arraylists to 0.0. shouldn't it be like this alla.get(i).setKurs(0.0); when I've found one stock object in my arraylist if the stock class (which is a subclass) have an public void setKurs(double kurs) { this.kurs = kurs; } ?

I'll post my code here:

This is my button

class börsLyssna implements ActionListener{
    public void actionPerformed(ActionEvent ave){
        for(int i=0; i<alla.size(); i++){
            if(alla.get(i).getClass().getName().equals("Aktie")){
                alla.get(i).setKurs(0.0);
            }
        }
    }
}

__

abstract class Värdesak{
private String namn;


protected Värdesak(String namn){
    this.namn = namn;

}

public String getNamn(){
    return namn;
}


abstract public double  getVärde();

public String toString(){
    return namn + ", "+ "värde: "+(getVärde()*1.25);
}
}

class Aktie extends Värdesak{
private int antal;
private double kurs;

public Aktie(String namn, int antal, double kurs){
    super(namn);
    this.antal = antal;
    this.kurs = kurs;
}

public double getVärde(){
    return (antal*kurs);
}



public String toString(){
    return super.toString()+", antal: "+antal+" med en kurs på: "+kurs;
}

public void setKurs(double kurs) {
    this.kurs = kurs;
}

public double getKurs() {
    return kurs;
}

}

I believe that the (Class).getName() method returns the fully qualified class name (including the package).

For example, from the JDK 1.8 docs, the getName() comments contains this example:

 String.class.getName()
     returns "java.lang.String"

You're testing for the Class name without the package hierarchy preface.

Instead of

alla.get(i).getClass().getName().equals("Aktie")

try

alla.get(i) instanceof Aktie

The alla.get(i).getClass().getName() will return the package and name of the class, eg java.lang.String instead of just String .

Since you don't show us how alla is populated we can't tell if any Aktie s are even in it. There are a lot of suggested ways to fix this. Here's some debugging code that will let you clearly know where the problem is and when it's fixed.

class börsLyssna implements ActionListener{
    public void actionPerformed(ActionEvent ave){
      for(int i=0; i<alla.size(); i++){
          if(alla.get(i).getClass().getName().equals("Aktie")){
              System.out.println("Attempting to set kurs to 0.0 at i=" + i");//TODO remove debugging code
              alla.get(i).setKurs(0.0);
          }
      }
   }
}

Personally I think alla.get(i) instanceof Aktie will fix it. Though I think your life will be easier if you didn't put different types of objects in a collection in the first place. Wouldn't it be nice if you could just call set() on everything in there?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM