user3066571
user3066571

Reputation: 1471

Return ArrayList from ArrayList method type

I'm making a little card deck program that uses an ArrayList for the deck. One of the limitations set upon me is that the method in which I "deal" the cards must be an Arraylist type. The problem I'm running into is that I don't know how to return just a specific index value from the ArrayList. See below.

public ArrayList deal(int n, boolean up){
    Card card0 = new Card();
    boolean cardFace = card0.state(up);
    return al.get(0);  //<-- This doesn't work, Netbeans says that it is a string type
                            //not an ArrayList type.  The only thing it will actually
                            //allow me to return is:
    return.al;  // But this doesn't work, I don't need to return the whole list,
                // just the first element, but Netbeans calls that a String type, not
                // ArrayList

So how can I return the first item of the List and still have it be the correct type? The rest of the code doesn't matter, just the Method type and return statement.

EDIT: As requested

package deckofcards;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class Deck{
    ArrayList<String> al = new ArrayList<>();
    public void shuffle(){
        Collections.shuffle(al);
    }
    public String displayDeck(){
        String returnDeck = "";
        for(int i = 0; i < al.size(); i++){
            String printDeck = al.get(i);
            returnDeck += printDeck;
        }
        return returnDeck;
    }
    public ArrayList deal(int n, boolean up){
        Card card0 = new Card();
        boolean cardFace = card0.state(up);
        return al.get(0);
    }
    public void populate(){
        al.add(0, "Ace of Spades");
        al.add(1, "Two of Spades");
        al.add(2, "Three of Spades");
        //yadaa yadaa

Upvotes: 4

Views: 53913

Answers (4)

//ADDING AND deleting employees //Displaying employee list

public class EployeeDB { static ArrayList e = new ArrayList<>();

public static boolean addEmployee(Employee e1) {

    e.add(e1);
    System.out.println("Employee added");
    return true;

}

public static boolean deleteEmployee(int ecode) {
    int temp = 0;
    for (int i = 0; i < e.size(); i++) {

        if (e.get(i).getID() == ecode) {
            temp = temp + 1;
            e.remove(i);
            break;
        }
    }
    if (temp == 1)
        System.out.println("Emp deleted");
    else
        System.out.println("Deletion unsuccessful, check ecode again");
    return true;

}

public static String showPaySlip(int ecode) {
    double salary = 0;
    int temp = 0;
    for (int i = 0; i < e.size(); i++) {
        if (e.get(i).getID() == ecode) {
            temp = temp + 1;
            salary = e.get(i).getSalary();
            break;
        }
    }
    if (temp == 1)
        return "Salary is" + salary;
    else
        return "No employye found with the specified ecode";

}

public static ArrayList<Employee> listAll() {

    return e;

}

public static void main(String[] args) {
    Employee e1 = new Employee();
    e1.setID(20);
    e1.setName("sai");
    e1.setSalary(150.00);
    addEmployee(e1);
    Employee e2 = new Employee();
    e2.setID(30);
    e2.setName("kumar");
    e2.setSalary(1500.00);
    addEmployee(e2);
    deleteEmployee(30);
    System.out.println(showPaySlip(30));

    for (int i = 0; i < e.size(); i++)
        System.out.println(
                listAll().get(i).getID() + " " + listAll().get(i).getName() + " " + listAll().get(i).getSalary());

}

}

Upvotes: 0

Juned Ahsan
Juned Ahsan

Reputation: 68715

If you cannot change the signature and it is mandatory to return an arraylist, then you can create an arraylist with just one element and return it. Something like this:

ArrayList returnList = new ArrayList();
returnList.add(al.get(0));
return returnList;

Does not look great to me :-(

Upvotes: 4

mreiss
mreiss

Reputation: 1

Is there a reason that you have to return an ArrayList? Essentially, you are trying to create a method that takes a deck, picks a card, and then returns a deck. You could try and use the subList method someone mentioned above. You could create a new ArrayList containing only the card you want, but that's not very efficient. Or, if your goal is to actually return the whole deck, but with the correct card on top (aka in the first position of the ArrayList), there's lots of info about rearranging values in an ArrayList online.

EDIT: Based on your full code, it looks like the goal is to flip the first card face up. You should do that (not gonna do your homework for you!) and then return the ArrayList that the method took in. IRL, imagine handing someone a deck, they flip the first card face up, then hand the deck back to you.

Upvotes: 0

Jason C
Jason C

Reputation: 40335

In your specific case, al is an ArrayList<String>. That means al.get(...) returns a String. However, your method is declared as returning an ArrayList, which is not a String. You will either need to change your method return type to String, or you will need to construct a new ArrayList and add your single string to it and return that.

Your declared return type needs to match the object you are returning. So for example:

 ArrayList<String> al = ...;

 String getSingleItem (int index) {
     return al.get(index);
 }

 ArrayList<String> getSingleItemAsArrayList (int index) {
     ArrayList<String> single = new ArrayList<String>();
     single.add(al.get(index));
     return single;
 }

 ArrayList<String> getItems () {
     return al;
 }

By the way, it's generally better to specify the type parameter to ArrayList, e.g. ArrayList<Whatever>, as this can save you a lot of casting things around / unchecked conversions and will give you compile-time checking of types.

Upvotes: 0

Related Questions