0

This is a part of my homework assignment so I need explanation and not just an answer.

I'm creating the legendary purse class. I have to be able to compare one purse(array) to another purse(array). The kicker is that I can't use any methods from the java arrays or collections class. Where do I even start on creating something like this?

An example of what im doing is this:

String[] array1 = {"Quarter", "Dime", "Nickel", "Penny"};
String[] array1 = { "Dime", "Quarter", "Penny", "Nickel"};
(Does Array1==Array2?)
return true/false

Again I need to understand this so please don't just figure it out for me, but throw me some ideas.

3
  • Those are not arrays. Those are Strings. Commented Mar 10, 2013 at 17:46
  • 1
    Do you mean String[] array1 = {"Quarter", "Dime", "Nickel", "Penny"};? Commented Mar 10, 2013 at 17:47
  • write a compare methd on you rpurse class. inside - loop on all the entries in some clever way (up to you) - and see if they all match. then return true or false. Commented Mar 10, 2013 at 17:49

2 Answers 2

1

The way you'd compare elements between two arrays without Arrays.equal() would be to iterate over each element and compare them one at a time.

String[] array1 = new String[] {"Quarter", "Dime", "Nickel", "Penny"};
String[] array2 = new String[] {"Dime", "Penny", "Quarter", "Nickel"};

public boolean equalArrays(String[] array1, String[] array2) {
    if(array1.length != array2.length) {
        return false;
    }
    int matched = 0;
    for(int i = 0; i < array1.length; i++) {
        for(int j = 0; j < array2.length; j++) {
            if(array2[j].equals(array1[i])) {
                matched++;
                break;
            }
        }
    }
    return matched == array1.length;
}
Sign up to request clarification or add additional context in comments.

2 Comments

"please don't just figure it out for me, but throw me some ideas"
The solution is an idea. I'm personally not a fan of doing it like this, but it's a way to think about it.
1

You could try nested for loops. In pseudo-code:

for each element 'i' in Array1:
    for each element 'j' in Array2:
        does 'i' equal 'j'?
            // do something
        else:
            // do something else

Does this get you started? Would you like more help?

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.