Lets say I have an ArrayList
called dictArray
that holds 5 words. The five words are listed as so {"aaron", "abates", "dog", "mellon", "zoo"}
. I need to loop through this string ArrayList one word at a time, and for each word create a ArrayList<Character> tempArray
to store only the unique characters within that word. (I only need to go one word at a time because I will be performing calculations on the tempArray within the for loop). For example, each iteration would return the following:
{a,r,o,n}
{a,b,t,e,s}
{d,o,g}
{m,e,l,o,n}
{z,o}
This block of code will successfully store only the unique characters of the subject word:
if(tempArray.contains(word.charAt(i))){
} else {
tempArray.add(word.charAt(i));
}
}
My question: How do I set up the for loop structure to go one word at time, and then after identifying the word, go one letter at a time to call the above if statement? Here is my current code but I am receiving the error message type of expression must be an array type but resolved to ArrayList<String>
underneath all of the j
's.
ArrayList<String> dictArray = new ArrayList<String>();
ArrayList<Character> tempArray = new ArrayList<Character>();
for (int i = 0; i < dictArray.size(); i++) {
for (j = 0; j < dictArray[j]; j++) {
if(tempArray.contains(word.charAt(i))){
} else {
tempArray.add(word.charAt(i));
}
}
}
This is your code with a better format, and I will go down line by line.
ArrayList<String> dictArray = new ArrayList<String>();
ArrayList<Character> tempArray = new ArrayList<Character>();
for (int i = 0; i < dictArray.size(); i++) {
for (j = 0; j < dictArray[j]; j++) {
if(tempArray.contains(word.charAt(i))){
} else {
tempArray.add(word.charAt(i));
}
}
}
Line 4: for (j = 0; j < dictArray[j]; j++) {
, if you want to access an element in the ArrayList, use get(int index). Also, you should use dictArray.get(i) instead of j (which you did not initialize). i is the index of String in the ArrayList, while j should be the index of characters in the String.
Line 5 - 8: can be combined together. You want to add the character if it is not in the tempArray so you don't need to use an if-else statement. Also, you never initialized word, which I think you meant dictArray.get(i)
Last I think you want to add the tempArray to a new ArrayList (call it answer) of Array of Strings which contains the unique characters for each word.
So here is the modified code
ArrayList<ArrayList<Character>> answer = new ArrayList<>();
for (int i = 0; i < dictArray.size(); i++) {
ArrayList<Character> tempArray = new ArrayList<Character>();
for (int j = 0; j < dictArray.get(i).length(); j++) {
if(!tempArray.contains(dictArray.get(i).charAt(j))){
tempArray.add(dictArray.get(i).charAt(j));
}
}
answer.add(tempArray);
}
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Test {
public static void main(String[] args) {
List<String> dictArray = Arrays.asList("aaron", "abates", "dog", "mellon", "zoo");
List<List<Character>> result = new ArrayList<>();
dictArray.stream().
forEach(word -> {
List<Character> charArray = new ArrayList<>();
for (Character c : word.toCharArray()) {
if (!charArray.contains(c)) {
charArray.add(c);
}
}
result.add(charArray);
});
System.out.println(result);
}
}
output :
[[a, r, o, n], [a, b, t, e, s], [d, o, g], [m, e, l, o, n], [z, o]]
you can use a for each to loop through the list and break the individuals down to char arrays and then loop through those
for (String s : arr ) {
char[] cArray = s.toCharArray();
for (char cs : cArray) {
// Your Logic Here
}
}
ArrayList
s need the get()
method to extract a particular value. It might also be helpful to learn about the foreach lop, given here by for(char c: tempArray)
. Basically, it means for every character given a variable name c
in the list tempArray
for(int i = 0; i < dictArray.size(); i++)
{
ArrayList<Character> tempArray = new ArrayList<Character>();
String val = dictArray.get(i);
for(int j = 0; j < val.length(); j++)
{
if(!tempArray.contains(val.charAt(j)))
{
tempArray.add(val.charAt(j));
}
}
String word = "";
for(char c: tempArray)
{
word += c;
}
dictArray.add(word);
}
This approach converts tempArray
to a String word
and stores it in dictArray
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.