0

How do you make this work? it says cannot find vaiable AL in function addint. how do I add int to Arraylist over different functions?

/**
 * Constructor
 */
public Player()
{
    ArrayList<int> AL = new ArrayList();
}

public void addint(int C){
    AL.add(C);
}
  • I think this kind of questions are divine interventions that test stack overflow community's faiths... – mostruash Jul 19 '13 at 21:38
  • @mostruash why do you think that? – aaronman Jul 19 '13 at 21:41
  • @aaronman Six answers and dozens of edits in a few seconds made me think that. I might be wrong. – mostruash Jul 19 '13 at 21:42
  • @mostruash someone had a hair-trigger on the downvote if the answer only addressed the generics problem which is why so many people took it upon themselves to answer – aaronman Jul 19 '13 at 21:44
  • ArrayList can't store primitives but only REFERENCES. To learn what is ArrayList please cover my tutorial on [Internal life of ArrayList](http://volodial.blogspot.com/2013/07/internal-life-of-arraylist-in-java.html) – Volodymyr Levytskyi Jul 20 '13 at 12:16

6 Answers6

7

You declared the variable AL inside the constructor (a local variable) and it is out of scope in the addInt method.

Declare AL outside of the constructor, but within the class. Also, use Integer, not the primitive type int for the generic type parameter. (Primitive types are not allowed as generic type parameters.)

public class Player
{
    // Declare it here.
    private ArrayList<Integer> AL;

    public Player()
    {
        // Initialize it here.
        AL = new ArrayList<Integer>();
    }
    // Now you can access `AL` in your methods.
    public void addint(int C){
        AL.add(C);
    }
}
rgettman
  • 176,041
  • 30
  • 275
  • 357
4

You should define the ArrayList<Integer> AL (not ArrayList<int>, you can't use primitive types in generics) as field of your class, you're currently defining it as parameter in your class constructor. Also, remember to always program to interfaces, not to class implementations, so declare the variable as List<Integer> instead.

public class Player {
    private List<Integer> AL;
    public Player() {
        AL = new ArrayList<Integer>();
    }
    public void addint(int C){
        AL.add(C);
    }
}

More info:

Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
3

The simplest way is:

public class Player {
    private final List<Integer> al = new ArrayList<Integer>();

    public void addint(int c){
        al.add(c);
    }
}
  • The variable must be reachable.
  • You don't need a constructor for this case, but you can if you want to.
  • We usually tend to referernce the interface List as it makes it easier to change the implementation later (check Joshua Bloch book)
  • You can't use primitive types as generics, but you can add a primitive to the list thanks to autoboxing.
  • You can use new ArrayList<>(); since Java7.
  • You should rather follow Java variable naming conventions. Uppercase generally means constants.
Sebastien Lorber
  • 89,644
  • 67
  • 288
  • 419
  • 1
    Keeping a constructor is good style and makes it easier to change instantiation later if necessary. – Kenogu Labz Jul 19 '13 at 21:35
  • @KenoguLabz that depends on design. – Luiggi Mendoza Jul 19 '13 at 21:35
  • 1
    @LuiggiMendoza Not really. A constructor is always more flexible than an in-place definition, and it helps separate your data from your logic cleanly. – Kenogu Labz Jul 19 '13 at 21:38
  • @KenoguLabz you're right, but if there's no need for such code, you can initialize it directly. Depending on some cases, the attributes will be set in an `init` method that will be called by the client of the class (so no need to initialize the attributes here either). It all depends on the design. – Luiggi Mendoza Jul 19 '13 at 21:44
  • A constructor with no args is kind of useless. You could also have used an init block if you need, but it would be useless too (and not the same init order, and more confusing for beginners). – Sebastien Lorber Jul 19 '13 at 21:49
  • It also looks like `al` could be `final` in this case. – Paul Bellora Jul 19 '13 at 22:27
2

You need to specify a field, and use <Integer> rather than <int>:

/**
 * Constructor
 */
ArrayList<Integer> AL;
public Player()
{
    this.AL = new ArrayList<Integer>();
}

public void addint(int C){
    this.AL.add(C);
}
jh314
  • 27,144
  • 16
  • 62
  • 82
1

In java you cannot use primitives like int in generics. You have to use Integer which is a wrapper class for int

ArrayList<Integer> AL = new ArrayList<Integer>();  

Also it seems that AL should be a member variable if you want it to work correctly not declared inside the constructor. I think this is what you want:

public class Player {
    private List<Integer> AL;
    public Player() {
        AL = new ArrayList<Integer>();
    }
    public void addint(int C){
        AL.add(C);
    }
}
Paul Bellora
  • 54,340
  • 18
  • 130
  • 181
aaronman
  • 18,343
  • 7
  • 63
  • 78
0
  1. You need to use the Wrapper class Integer instead of int while defining that ArrayList.

    ArrayList<Integer> AL = new ArrayList<Integer>();
    
  2. Make the ArrayList<Integer> AL an instance variable . Currently it is a local variable.

AllTooSir
  • 48,828
  • 16
  • 130
  • 164