1
public static getDeck(ArrayList<Integer> cards) {
    for (int total = 1; total !=5; total++) {
        for (int suit = 1; suit != 14; suit++) {
            cards.add(suit);
        }
    }
    Collections.shuffle(cards); // Randomize the list
    return cards;
}

public static void main(String[] args) {
    // Create the cards here.
    ArrayList<Integer> cards = new ArrayList<>();
    cards = getDeck(cards);
}

I would like to be able to call the function getDeck which will add 52 numbers to the Arraylist I pass to it. In this case cards. Then return this Object and set it to cards.

The error I am getting is so.

enter image description here

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Jack
  • 2,891
  • 11
  • 48
  • 65

4 Answers4

5

There is no return type for getDeck, you need to specify it as ArrayList<Integer> or any of its supertype in your case

public static ArrayList<Integer> getDeck(ArrayList<Integer> cards) {
    for (int total = 1; total !=5; total++) {
        for (int suit = 1; suit != 14; suit++) {
            cards.add(suit);
        }
    }
    Collections.shuffle(cards); // Randomize the list
    return cards;
}
mdolbin
  • 936
  • 4
  • 8
  • Ah thank you! I think this has done the trick! – Jack Feb 23 '14 at 15:48
  • FWIW, unless you want a "builder" pattern (like StringBuilder) it's cleaner to not return anything so it's obvious to the caller that you're going to mutate the passed in object. Like Collections.shuffle, you can tell it's going to mess with your ArrayList, because it doesn't return anything. – Ted Bigham Feb 23 '14 at 16:05
  • @TedBigham, This method surely have some design issues but it's completely separated topic that worth at least a few hours of studying. As for now there is a simple inattention. – mdolbin Feb 23 '14 at 16:13
1

You need to specify the return type of your method, like this:

public static ArrayList<Integer> getDeck(ArrayList<Integer> cards) {
    //your code
}
Salah
  • 8,567
  • 3
  • 26
  • 43
1

You forgot to include the return type. The method signature should be written like this:

public static List<Integer> getDeck(List<Integer> cards)

I would suggest using interface types, in this case List<Integer> type instead of the implementer type ArrayList<Integer>, in that way you could return all kind of implementors of List (e.g LinkedList, ArrayList and so on). It is a concept called program to interfaces.

Community
  • 1
  • 1
Bartzilla
  • 2,768
  • 4
  • 28
  • 37
  • 1
    +1 for program to the interface. This also applies to the method parameter, you could update this accordingly to be a `List`. – Marco13 Feb 23 '14 at 15:58
0

Program to interfaces:

public static List<Integer> getDeck(List<Integer> cards) {
    for (int total = 1; total !=5; total++) {
        for (int suit = 1; suit != 14; suit++) {
            cards.add(suit);
        }
    }
    Collections.shuffle(cards); // Randomize the list
    return cards;
}

public static void main(String[] args) {
    // Create the cards here.
    List<Integer> cards = new ArrayList<>();
    cards = getDeck(cards);
}
sadhu
  • 1,429
  • 8
  • 14