Hey all, this is indeed a homework question (I read the policy.) But I've tried everything and searched the web for ideas or functions with no luck.
Anyways, the basic part that I'm stuck at is I'm unable to find a way to count a number of times a character is in a string. like:
string blarg = "aabbccc"
counting the number of "b" would be 2
Now... the problem is Java doesn't like Strings versus Arrays, (unlike C/C++ where they are the same thing and I wouldn't be asking this question.)
So I tried converting it into an array, which worked, but then I was unable to count it. ( I've tried split and string.toCharArray)
count wise, using for loop and length of array. That part is not what I'm asking....
Pretty much I'm trying to find a command/method where I can compare two values of two separate arrays.
equals ( arr1 , arr 2) didn't work for me...
the == doesn't work for objects....
I don't think there is a generic way to compare two elements in arrays like that. how you would compare elements in an array of Integer objects would be completely different from how you would compare elements in an array of FooBar objects.
I guess the question I'd ask is "how do you want do to it"? you say you converted it to an array...but an array of WHAT? And then why were you unable to count it?
What kind of thing does your array hold? If they are objects, you would generally compare them like this:
that says "call the equals() method of the first object in arr1 and pass it the first object in arr2". If the equals method is written correctly, that should return true/false appropriately.
If your array contains primitives, you can just do
arr1 == arr2
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Tim Chaung wrote:. . . You can't use - if( arr1 == arr2 ) - In this cause you'd need the .equals
But you CAN use - if( arr1 == arr2 ) . . .
You have misunderstood your friend, I am afraid.
Use == only for primitives and members of an enum. You need the equals method for all reference types, which includes arrays and their non-primitive elements. But if you look in the Java Language Specification, you find the only overridden method in arrays is clone(). So myArray.equals(myOtherArray) uses the == operator internally. If you want another version of equals() look in the Arrays class. I can't link to the method directly, because of the  in it.
Yeah I JUST got the definition of primitives.... and I'm back to ground zero on the idea of counting the number of a certain character into a string...
So I has string blarg... convert it to a char array...
use charat to compare a element the char blarg to a character... <-- this is where I'm, stuck at.... again.... because it doesn't like the fact that the character "a" is a string. Would I then also convert that to a chararray?