Tuesday, 15 April 2014

Java - Checking sub array of chars ( contains some chars ? ) -



Java - Checking sub array of chars ( contains some chars ? ) -

in programme want create sure string (char array) has specific characters ( or specific counts of different chars , no more ) i've written code here problem not work correctly.

example : if have char array checking "hello" , want create sure has "ollhevi" or less should homecoming true.

or improve one: words = {'h','e','l','l','o'} wanted = {'a','o','h','l'} homecoming false because hello doesn't have 'a';

just in theory works.

code

public boolean check(arraylist<character> words, arraylist<character> wanted) { (character chr : words) { if (wanted.contains(chr)) { wanted.remove(chr); } else { homecoming false; } } homecoming true; }

run loop other way around.

run through characters in wanted array, , see if nowadays in words array. if of character missing, homecoming false.

public boolean check(arraylist<character> words, arraylist<character> wanted) { (character chr : wanted) { if (!words.contains(chr)) { homecoming false; } } homecoming true;

}

java arrays arraylist char

No comments:

Post a Comment