0

I have a question. I cant solve it and I need some help please. I have an Arraylist of objects then I have a method where objects are created and added to the Arraylist but I want another method where I can print the Arraylist but everytime I try the Arraylist is empty so this is my code:

public class Packages{
   ArrayList<Pack> myList = new ArrayList<Pack>();
   Pack obj;

   public double addPackage(int type, double num){
      if(type==1)
      {
         obj = new Pack(type, num);
         total = obj.calculateTotal;
      }
      else
      {
         obj = new Pack(type, num);
         total = obj.calculateTotal;
      }
      myList.add(obj);
      return total;
   }
   public int listSize(){
      return myList.size();
   }
}

Everytime I call the listSize() method it returns 0, looks like when the addPackage method finishes it deletes the objects I added to my Arraylist. Note: my addPackage method is going to return a double total but at the same time add the objects I create to the arraylist. I need some help please.

6
  • 3
    Show where you're calling both methods. Commented May 8, 2015 at 14:52
  • 6
    Also, do you realize that the if and the else do exactly the same thing? Why is obj a field? It should be a local variable. Commented May 8, 2015 at 14:55
  • 2
    Your code, or at least what's shown, will work fine, though it's a bit odd, since I assume calculateTotal should be a method, not a property, obj should be a local variable, your if/else is totally redundant, etc. Commented May 8, 2015 at 14:56
  • Pack is another class and calculateTotal is a method from the Pack class. and addPackage and listSize methods are being called in a driver class. calculateTotal just returns the num + something else. type variable can be 1 or 2. Commented May 8, 2015 at 15:09
  • Though the code does not compile I have taken the liberty to make some changes and now it seems to work. Commented May 8, 2015 at 15:23

1 Answer 1

2

I tried your code and it is almost right. I am posting the classes again which I used and which work:

public class Package {
    List<Pack> myList = new ArrayList<Pack>();
    Pack obj;
    double total = 0;

    public double addPackage(int type, double num) {
        if (type == 1) {
            obj = new Pack(type, num);
            total = obj.calculateTotal();
        } else {
            obj = new Pack(type, num);
            total = obj.calculateTotal();
        }
        myList.add(obj);
        return total;
    }

    public int listSize() {
        return myList.size();
    }
}

Now class Pack is:

public class Pack {
    int type;
    double value;

    public Pack(int type, double value) {
        this.type = type;
        this.value = value;
    }

    public double getValue() {
        return value;
    }

    public void setValue(double value) {
        this.value = value;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public double calculateTotal() {
        return type*value;
    }
}

And I verified in this code:

public static void main(String[] args) {
        Package pkg = new Package();
        pkg.addPackage(10,10);
        pkg.addPackage(10,20);
        System.out.println(pkg.listSize());

    }

And as expected it returns 2. All these classes may not exactly be same as what you have but it will give you the idea about what are you missing.

Sign up to request clarification or add additional context in comments.

3 Comments

you gave me an idea I think the problem is in my driver class. what if I have a GUI class and I add the data just like you did, but instead I use 2 Jtextfields and a button to addpackage(which calls addPackage method) and another button to print the arraysize(which calls listSize method) do you thing everytime I click addpackage button it resets the previous data??
If those two buttons invoke method on same instance of Package then it should work provided code is written correctly.
it works now thank you!! I was creating the Package object inside the buttonlistener that was my mistake. it was so simple...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.