0

I have the following methods that I wrote are array and boolean methods. I was wondering how, if possible, can I convert the array method into an ArrayList one.

public int[] letsMap(){

    int[] iArray = new int[maxBagSize + 1];

    int w;

    for(int i = 0; i < ourBags.size(); i++){
        w = ourBags.get(i).foo;
        iArray[w]++;
    }

    return iArray;

}

.

public boolean comp(SearchState search){
            boolean cool = false;

            if(search.unBaggedItems.equals(this.unBaggedItems)){
                int[] mine = letsMap();
                int[] theirs = search.letsMap();

                cool= true;

                for(int i = 0; i < maxBagSize; i++){
                    if(mine[i] != theirs[i]){
                        cool = false;
                        break;
                    }
                }
            }

            return cool;
        }
2
  • Converted the int[] to ArrayList<>() but got lost in terms of knowing what to do with iArray[w]++; Commented Dec 23, 2013 at 5:42
  • What exactly is this supposed to do? If any of your elements in ourBags meets or exceeds the length of the array, you'll get a runtime exception. Also, what does your method comp have to do with your question? Commented Dec 23, 2013 at 5:52

1 Answer 1

1

For the first method:

public ArrayList<Integer> letsMap(){

    ArrayList<Integer> iArray = new ArrayList<>(maxBagSize + 1);

    int w;

    for(int i = 0; i < ourBags.size(); i++){
        w = ourBags.get(i).foo;
        iArray.set(w, iArray.get(w) + 1);
    }

    return iArray;

}

And for the second:

public boolean comp(SearchState search){
    boolean cool = false;

    if(search.unBaggedItems.equals(this.unBaggedItems)){
        ArrayList<Integer> mine = letsMap();
        ArrayList<Integer> theirs = search.letsMap();

        cool = true;

        for(int i = 0; i < maxBagSize; i++){
             if(mine.get(i).compareTo(theirs.get(i)) != 0){
                cool = false;
                break;
            }
        }
    }

    return cool;
}
Sign up to request clarification or add additional context in comments.

Comments

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.