9 Replies Latest reply: Feb 9, 2009 5:48 PM by 807588 RSS

    ArrayList  .set() method error -- need some help.

    807588
      I am doing an assignment about generic classes but when i compile this i get an error:

      MyStatisticsClass.java:21: cannot find symbol
      symbol : method set(int,double)
      location: class java.util.ArrayList<T>
                     list.set(i, temp);
                     ^

      heres the code:

      import java.util.*;
      public class MyStatisticsClass<T extends Number>{
           private ArrayList<T> list = new ArrayList<T>();
           //private ArrayList<T> newList = new ArrayList<T>();
           
           public MyStatisticsClass(ArrayList<T> data){
                list = data;
           }
           public double standardDeviation(){
              double temp;
                double temp2;
                //Get double value of all numbers.
                for(int i = 0; i<list.size(); i++){
                     temp = list.get(i).doubleValue();
                     list.set(i, temp);
                }
                //Inside the square root
                for(int i = 0; i<list.size(); i++){
                     temp = list.get(i).doubleValue();
                     temp =- this.average();
                     temp = temp * temp;
                     temp2 =+ temp;
                }
                temp = temp2/list.size();
                
                //Square root and return
                return Math.sqrt(temp);
                
           }
           public double average(){
                double temp;
                double num;
                for(int i = 0; i<list.size(); i++){
                     temp =+ list.get(i).doubleValue();
                     num++;
                }
                return temp/num;
           }
      }
      Any help on how to fix this.. i looked at the method and it looks like i have the right package imported and correct variable types for the index and data .. idk why it wont work.
        • 1. Re: ArrayList  .set() method error -- need some help.
          807588
          BTW, =+ is not +=

          =- is not -=

          I think in the early days of C, or B or BCPL, they did use the notation =- for that assignment operator. But it's too close to a typo:
          x=- 1; //x= x-1;
          x=-1; //x= x-1; ? x= -1; ?
          x= -1;// ;-)
          • 2. Re: ArrayList  .set() method error -- need some help.
            807588
            Even if this did work:
            for(int i = 0; i<list.size(); i++){
                temp = list.get(i).doubleValue();
                list.set(i, temp);
            }
            Why are you doing it? What do you expect it to do?
            • 3. Re: ArrayList  .set() method error -- need some help.
              807588
              Also, I don't imagine the average will change while you are calculating the standard deviation. So why are you recalculating it at every index?
              temp -= this.average();
              • 4. Re: ArrayList  .set() method error -- need some help.
                JoachimSauer
                bardo23 wrote:
                I am doing an assignment about generic classes but when i compile this i get an error:

                MyStatisticsClass.java:21: cannot find symbol
                symbol : method set(int,double)
                location: class java.util.ArrayList<T>
                               list.set(i, temp);
                               ^
                Ok, this might get confusing.

                Let's take a look at your "list":
                private ArrayList<T> list = new ArrayList<T>();
                That means that it is an "ArrayList<T>" (btw, you should change that to just List<T>, but keep the "new ArrayList<T>()". That's called programming against the interface).

                Now let's take a look how "T" is defined:
                public class MyStatisticsClass<T extends Number>{
                T is defined as "T extends Number".

                Now we know that your list is a List of objects of a type T and the type T is some sub-class of Number.

                Now let's look at what methods a [List<T>|http://java.sun.com/javase/6/docs/api/java/util/List.html] provides (hint: an ArrayList<T> provides the same ones plus a few more, that we don't care about right now):

                Look, there's [set(int index, E element)|http://java.sun.com/javase/6/docs/api/java/util/ArrayList.html#set(int,%20E)]! That looks about right.

                Now what is that E? It's a type parameter of List and in our example it's specified by T, so for the moment we can assume that E = T.

                So you've got a method that looks something like this:

                set(int index, T element);

                Now you're trying to call that method with the parameters "i" and "temp".

                "i" is an int, so that's ok.
                "temp" is a double. "double" and "T" is not the same thing.

                This means that this set() method is not the correct one. Since there is no other set() method Java tells you that it can't find the symbol.
                • 5. Re: ArrayList  .set() method error -- need some help.
                  807588
                  In the assignment we are suppose to be able to use a list w any subclass of Number but then we are to use that method to get the double values for all the numbers in the list. I am trying to take each number and get the double value of it and place it back into the list. Such as taking a 3 (as int) and making it 3.0 (as double).
                  • 6. Re: ArrayList  .set() method error -- need some help.
                    807588
                    This makes sense to me. Thanks. Going to try to find away to get it to work as the assignment specifies, and idea or tips are still welcome.
                    • 7. Re: ArrayList  .set() method error -- need some help.
                      JoachimSauer
                      bardo23 wrote:
                      In the assignment we are suppose to be able to use a list w any subclass of Number but then we are to use that method to get the double values for all the numbers in the list. I am trying to take each number and get the double value of it and place it back into the list. Such as taking a 3 (as int) and making it 3.0 (as double).
                      1.) T can be any subclass of Number.
                      2.) Those subclasses can be Integer and Double, for example.
                      3.) Those subclasses can't be int or double. Those are primitive types and have nothing to do with generics at all.
                      4.) Your list contains elements of some type T.
                      5.) You can't create objects of type T (since you don't know the concrete type when you write it)
                      6.) Therefore you can't add any elements of type T to the list, that you create (because you can't create them).

                      Does the homework description require that you add elements to the list? Because it would be pretty hard and not really helpful to give this as a task.

                      For the moment you should assume that the list is read-only and that you can't add anything or change any value in it. It will simplify your task a lot.
                      • 8. Re: ArrayList  .set() method error -- need some help.
                        807588
                        bardo23 wrote:
                        In the assignment we are suppose to be able to use a list w any subclass of Number but then we are to use that method to get the double values for all the numbers in the list. I am trying to take each number and get the double value of it and place it back into the list. Such as taking a 3 (as int) and making it 3.0 (as double).
                        I still doubt this is a good thing to do, but if you want to produce a list of Doubles from a list of Numbers, create a proper new list:
                        List<Double> dlist = new ArrayList<Double>(list.size());
                        for (Number x : list) {
                            dlist.add(x.doubleValue());
                        }
                        I claim you should stick with the List<Number> and just use doubleValue().
                        • 9. Re: ArrayList  .set() method error -- need some help.
                          807588
                          Ah i see what you are talking about now. I was being very redundant.. trying to make them all doubles then in the next loop when i call them i use the same method to get the double value again. Thanks. I was a little confused at first.