简体   繁体   中英

How to cast from class type object to double type in java

I have a method to subtract two values from two different list which are of class type.The code for subtraction is

public LinkedHashMap<String, List<Double>> diff()
  { 
      List<Double> z=new ArrayList<Double>();
     for(int i=0;i<st_jsp.size()&&i<ref_jsp.size();i++){
         Double x=(double) ref_jsp.get(i);
         Double y=st_jsp.get(i);

          if(x>y){
              z.add(x-y);
          }
          else{
          z.add(y-x);
          }

          dif.put("", z);
       }

    return dif;

  }

Here st_jsp and ref_jsp are two list such as List<Ref_user_interface> st_jsp=new ArrayList<Ref_user_interface>(); and List<Ref_user_interface> st_jsp=new ArrayList<Ref_user_interface>(); Ref_user_interface is my class name. Thses two list holds value of in following way-

 ref.setSt1_vs1_bag6_rb(rs.getDouble(8));
 ref.setSt1_vs1_bag7_rb(rs.getDouble(9));
 ref.setSt1_vs1_bag8_rb(rs.getDouble(10));
 st_jsp.add(ref);

where ref is Ref_user_interface ref=new Ref_user_interface(); In my subtract method ,I'm getting an ** cannot convert from Ref_user_interface to Double** on line Double x= ref_jsp.get(i);Double y=st_jsp.get(i); . Is there any way through which I can subtract my values from two list whcih hold data of object type.?

EDIT-1 I have getter and setter methods.But for each value of list,Do I have to include getter methods??For my getter and setter method please visit the link setter method of mine

EDIT-2

I have changed my subtraction method by all your answers as-

public LinkedHashMap<String, List<Double>> diff()
  {  

     List<Double> z=new ArrayList<Double>();

     for(int i=0;i<st_jsp.size()&&i<ref_jsp.size();i++)
     {
            Comaprision refObj = ref_jsp.get(i);
            Comaprision stObj = st_jsp.get(i);
            Double x =refObj.getBeam_current();
            x+=refObj.getBeam_energy();
            x+=refObj.getSt2_vs2_bag1_rb();
            x+=refObj.getSt2_vs2_bag2_rb();
            x+=refObj.getSt2_vs2_bag3_rb();
            x+=refObj.getSt2_vs2_bag4_rb();
            x+=refObj.getSt2_vs2_bag5_rb();
            x+=refObj.getSt2_vs2_bag6_rb();
            x+=refObj.getSt2_vs2_bag7_rb();
            x+=refObj.getSt2_vs2_bag8_rb();
            x+=refObj.getSt2_vs2_bag9_rb();
             Double y = stObj.getBeam_current();
            y+=stObj.getBeam_energy();
            y+=stObj.getSt2_vs2_bag1_rb();
            y+=stObj.getSt2_vs2_bag2_rb();
            y+=stObj.getSt2_vs2_bag3_rb();
            y+=stObj.getSt2_vs2_bag4_rb();
            y+=stObj.getSt2_vs2_bag5_rb();
            y+=stObj.getSt2_vs2_bag6_rb();
            y+=stObj.getSt2_vs2_bag7_rb();
            y+=stObj.getSt2_vs2_bag8_rb();



          if((x>y)){

              z.add(x-y);
          }
          else{
          z.add(y-x);

          dif.put("", z);

       }
    return dif;

  }

but I can't understand that whether all the values at 1 index are being added and then comapred with y at index 1 and so on .Moreover this loop gives me 42 values.My ref_jsp has 21 rows and st_jsp has 78 rows but value of these rows are being repeated twice.I can't understand this for loop .Please help me to explain.I don't want to add up my values at a index,

My actual problem is to --> I just want to compare value at getter of each index of x with value at getter of each index of y and find the difference.

the thing is your list returns the object of Ref_user_interface so you can get value by calling the getter method like

ref_jsp.get(i).getSt1_vs1_bag6_rb()

and if you want all the getter methods the try this in your for loop

for(int i=0;i<st_jsp.size()&&i<ref_jsp.size();i++){
    Ref_user_interface refObj = ref_jsp.get(i);
    Ref_user_interface stObj = st_jsp.get(i);
    Double x = refObj.getSt1_vs1_bag6_rb();
    Double y = stObj.getSt1_vs1_bag6_rb();
    comparing(x,y);
    x =refObj.getSt1_vs1_bag7_rb();
    y = stObj.getSt1_vs1_bag7_rb();
    comparing(x,y)

     // and so on for all the getters method you have for this obj

     public void comparing(Double x,Double y){
           // write your comparing logic here and add value in map
          if(x > 0 && y > 0){
              if(x>y){
                 z.add(x-y);  
              }else{
                 z.add(y-x); 
              }  
          }else if(x>0){
             z.add(x);
          }else{
             z.add(y);
          }     
     }

and so on you get all the values you want and then use it according to your logic.

You need a getter like this setter ref.setSt1_vs1_bag6_rb(rs.getDouble(8)); . When you call ref_jsp.get(i) it returns Ref_user_interface then you need to call the new getter method to get what you want.

Update:

Double[] dbl_x = new Double[ref_jsp.size()];
Double[] dbl_y = new Double[ref_jsp.size()];
for(int i=0; i<ref_jsp.size(); i++){
   dbl_x[i] = ref_jsp.get(i).getSt1_vs1_bag6_rb();
}

for(int i=0; i<ref_jsp.size(); i++){
   dbl_y[i] = st_jsp.get(i).getSt1_vs1_bag6_rb();
}

At the end of this you have two arrays dbl_x and dbl_y and these arrays contain x and y values (which you referred earlier)

Then all you have to do is compare that two array and take decisions.

for(){
   if(dbl_x[i] > dbl_y[i]){
     // do this.
   }else{
     // do that.
   }
}

Above code snippet will give you a double array with the values you required. Do this for other set of values and the compare two arrays.

Note: getSt1_vs1_bag6_rb() is the getter method which returns the required values. Read about getters here

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