This site requires JavaScript, please enable it in your browser!
Greenfoot back
doglover555
doglover555 wrote ...

2017/12/2

ArrayList Help

doglover555 doglover555

2017/12/2

#
How do I check to see if two Lists are the same?
danpost danpost

2017/12/2

#
doglover555 wrote...
How do I check to see if two Lists are the same?
If the elements of one list is contained in the other and all the elements of the other are contained in the first one, then they both contain the same things (not necessarily in the same order; duplicates items may not match one for one).
doglover555 doglover555

2017/12/2

#
But is there a way to check it within my code? Like if the dog description is brown and spotted and the user picks a white and spotted dog is there a method or something that would return false in that case (descriptions are already set in Lists - one for projected pet and one for the pet the owner picks)?
danpost danpost

2017/12/2

#
doglover555 wrote...
But is there a way to check it within my code? Like if the dog description is brown and spotted and the user picks a white and spotted dog is there a method or something that would return false in that case (descriptions are already set in Lists - one for projected pet and one for the pet the owner picks)?
I would need to see how these lists are created, how your descriptive data is stored, etc.
doglover555 doglover555

2017/12/2

#
First list:
import greenfoot.*;
import java.util.*;

public class PackingList extends Actor
{

    private static List<Items> packingList;
    private int top;
    private int bottom;
    private int accessory;
    private String packingListOutput = "";
    
    public PackingList()
    {
        packingList = new ArrayList<Items>();
        setPackingList();
    }
    public static List<Items> getPackingList()
    {
        return packingList;
    }
    public String setPackingList()
    {
        Random rand = new Random();
        this.top = rand.nextInt(3);
        this.bottom = rand.nextInt(2);
        this.accessory = rand.nextInt(3);
        
        if(this.top == 0)
        {
            this.packingListOutput += "t-shirt, ";
            packingList.add(new TShirt());
        }
        else if(this.top == 1)
        {
            this.packingListOutput += "tank, ";
            packingList.add(new Tank());
        }
        else
        {
            this.packingListOutput += "";
        }
        
        if(this.bottom == 0)
        {
            this.packingListOutput += "skirt, ";
            packingList.add(new Skirt());
        }
        else
        {
            this.packingListOutput += "shorts, ";
            packingList.add(new Shorts());
        }
        
        if(this.accessory == 0)
        {
            this.packingListOutput += "sunglasses, ";
            packingList.add(new Sunglasses());
        }
        else if(this.accessory == 1)
        {
            this.packingListOutput += "necklace, ";
            packingList.add(new Necklace());
        }
        else
        {
            this.packingListOutput += "";
        }
        return this.packingListOutput;
    }
}
Second list: In Mannequin class (extends Actor) -
private List<Items> guess;

    public void checkPackingList()
    {
        guess = getIntersectingObjects(Items.class);
        //figure out how to see if the list are equal
        PackingList.getPackingList();
        
    }
You drag the items onto the mannequin so the first list is the items you are supposed to drag onto the mannequin and the second list is the items the player put on the mannequin so I need a way to check to see if the two lists are the same to make sure the player put the right items on the mannequin.
danpost danpost

2017/12/3

#
Okay -- your lists will never contain the same things. The reason being is that you create and add items to one list (in the PackingList class), then, you acquire the other list by using 'getIntersectingObjects' (in the Mannequin class). These lists may contain similar type items, but they will not be the same items (a Tank actor in one list is not the same Tank actor in the other list). Probably best is to create one of each item to start with. Save a reference to each and add a getter method for each. Then the world can get and add each into itself and the PackingList class can add their references to its list. That way, the intersecting objects will be the exact same items.
You need to login to post a reply.