Click to See Complete Forum and Search --> : JAVA problem
chronicle
04-21-2003, 09:53 AM
Hi
Having some trouble with JAVA.
I'm writing a little game and I need to compare an array with "chars" sort of to it self. So if I run an array with these inputs "QWERTYT" (Q is the #0 in the array, W 1 etc)it should end up with something like this "QWERTY*". SO it takes away all letters that have a duplicate. I can't get it to work, so please help.
thanks
cjohnson
04-21-2003, 08:06 PM
Okay, I'm not sure I totally follow what you mean, but it sounds like you're just looking for duplicates in a char[] array. Just traverse the array, starting at the beginning, keep track of the character you're looking for, and just replace it with asterisks (I'm assuming that's what you want, given the example)... This is really ad hoc, but...
public class DuplicateElimination {
public static char[] removeDuplicates(char[] array) {
char[] retVal = (char[]) array.clone();
for (int i = 0; i < retVal.length; i++) {
char current = retVal[i];
for (int j = 0; j < i; j++) {
if (retVal[j] == current)
retVal[j] = '*';
}
for (int j = i + 1; j < retVal.length; j++) {
if (retVal[j] == current)
retVal[j] = '*';
}
}
return retVal;
}
public static void main(String[] args) {
char[] foo = new char[]{'Q', 'W', 'E', 'R', 'T', 'Y',
'D', 'V', 'O', 'R', 'A', 'K'};
System.out.println(DuplicateElimination.removeDuplicates(foo ));
}
}
Which outputs 'QWERTYDVO*AK'.
This does look like a good solution. But I have a couple observations. First, there's no reason to clone the input array. You can just work with the "array" variable throughout and return it at the end. Second, you don't need that first loop (int j=0; j<1; j++). You only need to loop from the current position+1 to the end (as the second loop does). Finally, if curret == '*', there's no need to run the inside loop at all.
These suggestions worked into the original solution are:
public class DuplicateElimination {
public static char[] removeDuplicates(char[] array) {
for (int i = 0; i < array.length; i++) {
char current = array[i];
if (current != '*') {
for (int j = i + 1; j < array.length; j++) {
if (array[j] == current) {
array[j] = '*';
}
}
}
}
return array;
}
public static void main(String[] args) {
char[] foo = new char[]{'Q', 'W', 'E', 'R', 'T', 'Y',
'D', 'V', 'O', 'R', 'A', 'K'};
System.out.println(DuplicateElimination.removeDuplicates(foo ));
}
}
It's always easier to critique than to write :D
EverlastingGod
04-22-2003, 02:27 PM
Probably want to clone the input array, if you don't want the input array modified.
chronicle
04-22-2003, 03:33 PM
Originally posted by cjohnson
Okay, I'm not sure I totally follow what you mean, but it sounds like you're just looking for duplicates in a char[] array. Just traverse the array, starting at the beginning, keep track of the character you're looking for, and just replace it with asterisks (I'm assuming that's what you want, given the example)... This is really ad hoc, but...
public class DuplicateElimination {
public static char[] removeDuplicates(char[] array) {
char[] retVal = (char[]) array.clone();
for (int i = 0; i < retVal.length; i++) {
char current = retVal[i];
for (int j = 0; j < i; j++) {
if (retVal[j] == current)
retVal[j] = '*';
}
for (int j = i + 1; j < retVal.length; j++) {
if (retVal[j] == current)
retVal[j] = '*';
}
}
return retVal;
}
public static void main(String[] args) {
char[] foo = new char[]{'Q', 'W', 'E', 'R', 'T', 'Y',
'D', 'V', 'O', 'R', 'A', 'K'};
System.out.println(DuplicateElimination.removeDuplicates(foo ));
}
}
Which outputs 'QWERTYDVO*AK'.
I'm very thankful 4 your answers but what if I wanted to test it for non-capital letters also, what would you need to change?
QWERTrDVORAr -> QWERT*DVO*A*
or like
Babar -> Ba**r
EverlastingGod
04-22-2003, 04:56 PM
http://java.sun.com/j2se/1.4.1/docs/api/java/lang/Character.html
You'll find the methods of the Character class useful.
Originally posted by EverlastingGod
Probably want to clone the input array, if you don't want the input array modified. Ah, but this wasn't part of the initial request ;)
Maybe I've been reading too many engagement letters recently...
EverlastingGod
04-23-2003, 10:17 AM
Originally posted by rock
Ah, but this wasn't part of the initial request ;)
But it wasn't not part of the initial request ;)