[英]How to create a loop through LinkedHashMap<String,ArrayList<String>>?
[英]How to loop through a string ArrayList and create temporary Character Arrays of each word in the String ArrayList
假設我有一個名為dictArray
的ArrayList
,它包含 5 個單詞。 這五個詞被列為 so {"aaron", "abates", "dog", "mellon", "zoo"}
。 我需要一次遍歷這個字符串 ArrayList 一個詞,並為每個詞創建一個ArrayList<Character> tempArray
以僅存儲ArrayList<Character> tempArray
中的唯一字符。 (我一次只需要執行一個單詞,因為我將在 for 循環中對 tempArray 執行計算)。 例如,每次迭代將返回以下內容:
{a,r,o,n}
{a,b,t,e,s}
{d,o,g}
{m,e,l,o,n}
{z,o}
此代碼塊將成功僅存儲主題詞的唯一字符:
if(tempArray.contains(word.charAt(i))){
} else {
tempArray.add(word.charAt(i));
}
}
我的問題:如何設置for循環結構一次走一個詞,然后識別出這個詞后,一次走一個字母來調用上面的if語句? 這是我當前的代碼,但我收到錯誤消息type of expression must be an array type but resolved to ArrayList<String>
在所有j
。
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));
}
}
}
這是你的代碼格式更好,我會一行一行地往下看。
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));
}
}
}
第 4 行: for (j = 0; j < dictArray[j]; j++) {
,如果要訪問 ArrayList 中的元素,請使用 get(int index)。 此外,您應該使用 dictArray.get(i) 而不是 j (您沒有初始化)。 i 是 ArrayList 中 String 的索引,而 j 應該是 String 中字符的索引。
第 5 - 8 行:可以組合在一起。 如果字符不在 tempArray 中,您想添加它,因此您不需要使用 if-else 語句。 另外,您從未初始化過單詞,我認為您的意思是 dictArray.get(i)
最后,我認為您想將 tempArray 添加到包含每個單詞的唯一字符的字符串數組的新 ArrayList(稱為答案)中。
所以這是修改后的代碼
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);
}
}
輸出 :
[[a, r, o, n], [a, b, t, e, s], [d, o, g], [m, e, l, o, n], [z, o]]
您可以使用 for each 遍歷列表並將個體分解為字符數組,然后遍歷這些數組
for (String s : arr ) {
char[] cArray = s.toCharArray();
for (char cs : cArray) {
// Your Logic Here
}
}
ArrayList
需要get()
方法來提取特定值。 了解這里由for(char c: tempArray)
給出的 foreach lop 也可能會有所幫助。 基本上,這意味着對於列表tempArray
給定變量名稱c
每個字符
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);
}
這種方法將tempArray
轉換為 String word
並將其存儲在dictArray
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.