JAVA用对象填充临时Arraylist,然后将其添加到由Arraylist组成的Arraylist中
我遍历由对象组成的数组。我想找到这个数组的某些部分,其中相同的特定对象彼此跟随 [a,b,c,,,,h,g,a,,,s,,,h]->我想找到,和,是特定的对象 我已经知道了如何做到这一点:如果我找到了一个,我将把这个对象添加到一个临时的ArrayList中。如果下一个对象也是,我也会添加它,否则我会清除tmplist,因为它是单个对象。如果下一个对象不是,但tmplist大于1,我想将tmplist添加到一个2d ArrayList,一个由ArrayList组成的ArrayList,然后清除tmplist,以便找到其他部分 这是我的问题:如果我这样做,2d arraylist不包含templists的deepcopy->2d arraylist包含空列表,因为我会在找到每个模式后清除tmplist。我怎样才能解决这个问题 一些代码可以更好地解释它:JAVA用对象填充临时Arraylist,然后将其添加到由Arraylist组成的Arraylist中,java,arrays,list,multidimensional-array,arraylist,Java,Arrays,List,Multidimensional Array,Arraylist,我遍历由对象组成的数组。我想找到这个数组的某些部分,其中相同的特定对象彼此跟随 [a,b,c,,,,h,g,a,,,s,,,h]->我想找到,和,是特定的对象 我已经知道了如何做到这一点:如果我找到了一个,我将把这个对象添加到一个临时的ArrayList中。如果下一个对象也是,我也会添加它,否则我会清除tmplist,因为它是单个对象。如果下一个对象不是,但tmplist大于1,我想将tmplist添加到一个2d ArrayList,一个由ArrayList组成的ArrayList,然后清除tm
List<Object> tmplist = new ArrayList<Object>();
for (int i = 0; i<array.length(); i++) {
if (array[i].equals(#)) {
tmplist.add(array[i]);
if (!array[i+1].equals(#) && tmplist.size() < 2){
tmplist.clear();
} else if (!array[i+1].equals(#) && tmplist.size() > 1) {
pattern.add(tmplist);
tmplist.clear();
}
}
}
//pattern is the 2d ArrayList (ArrayList which consists of ArrayLists)
而不是执行tmplist.clear do tmplist=new ArrayList,这样每次都可以使用不同的列表实例。而不是执行tmplist.clear do tmplist=new ArrayList,这样每次都可以使用不同的列表实例。我认为最简单的解决方案是不清除列表,但在将ArrayList添加到列表后,将其新引用分配给tmplist。我认为最简单的解决方案是不清除列表,而是在将ArrayList添加到列表后将其新引用分配给tmplist。如果2d ArrayList为:result 做 这样做并不是添加tmpList本身,而是添加一个包含tmpList值的新列表。因此,即使执行tmpList.clear,也不会影响结果中的arraylist。如果2d arraylist为:result 做
这样做并不是添加tmpList本身,而是添加一个包含tmpList值的新列表。因此,即使您执行tmpList.clear,它也不会影响结果中的arraylist。好的。我知道您现在一定已经使用建议解决了这个问题。。。。但我只是想我会做一个程序来做这个。。。这是我的解决方案,可能不是最好的解决方法,但应该有效
import java.util.ArrayList;
import javax.swing.text.rtf.RTFEditorKit;
public class ArrTest {
public static void main(String[] args) {
String[] text = new String[] { "a","#","#","b", "c", "#", "#","#", "b", "3", "3" };
Map<Integer, List<String>> retMap = new HashMap<Integer, List<String>>();
List<String> repeatString;
int count = 0;
for (int i = 1; i < text.length; i++) {
if(text[i].equals(text[i-1])){
repeatString = new ArrayList<String>();
repeatString.add(text[i]);
for(int j=i;j<text.length;j++){
if(text[j].equals(text[i])){
repeatString.add(text[j]);
} else {
break;
}
}
retMap.put(count, repeatString);
count++;
i++;
}
}
Iterator<Integer>iter = retMap.keySet().iterator();
while(iter.hasNext()){
System.out.println(retMap.get(iter.next()));
}
}
}
希望这有助于:
输出:
[,]
[, ]
[3,3]好的..我知道你现在一定已经用建议解决了这个问题。。。。但我只是想我会做一个程序来做这个。。。这是我的解决方案,可能不是最好的解决方法,但应该有效
import java.util.ArrayList;
import javax.swing.text.rtf.RTFEditorKit;
public class ArrTest {
public static void main(String[] args) {
String[] text = new String[] { "a","#","#","b", "c", "#", "#","#", "b", "3", "3" };
Map<Integer, List<String>> retMap = new HashMap<Integer, List<String>>();
List<String> repeatString;
int count = 0;
for (int i = 1; i < text.length; i++) {
if(text[i].equals(text[i-1])){
repeatString = new ArrayList<String>();
repeatString.add(text[i]);
for(int j=i;j<text.length;j++){
if(text[j].equals(text[i])){
repeatString.add(text[j]);
} else {
break;
}
}
retMap.put(count, repeatString);
count++;
i++;
}
}
Iterator<Integer>iter = retMap.keySet().iterator();
while(iter.hasNext()){
System.out.println(retMap.get(iter.next()));
}
}
}
希望这有助于:
输出:
[,]
[, ]
[3,3]您的代码,根据@heeneee的答案修改:
List<Object> tmplist = new ArrayList<Object>();
for (int i = 0; i<array.length(); i++) {
if (array[i].equals(#)) {
tmplist.add(array[i]);
if (!array[i+1].equals(#) && tmplist.size() < 2) {
tmplist.clear();
} else if (!array[i+1].equals(#) && tmplist.size() > 1) {
pattern.add(tmplist);
tmplist = new ArrayList<Object>(); // Easy changes here
}
}
}
//pattern is the 2d ArrayList (ArrayList which consists of ArrayLists)
根据@heeneee的答案修改您的代码:
List<Object> tmplist = new ArrayList<Object>();
for (int i = 0; i<array.length(); i++) {
if (array[i].equals(#)) {
tmplist.add(array[i]);
if (!array[i+1].equals(#) && tmplist.size() < 2) {
tmplist.clear();
} else if (!array[i+1].equals(#) && tmplist.size() > 1) {
pattern.add(tmplist);
tmplist = new ArrayList<Object>(); // Easy changes here
}
}
}
//pattern is the 2d ArrayList (ArrayList which consists of ArrayLists)
如果你遇到了[a,b,c,,,,]。你的代码一定会因IndexOutOfBoundsException而爆炸!如果你遇到了[a,b,c,,,,]。你的代码一定会因IndexOutOfBoundsException而爆炸@彼得111我很高兴它有帮助:@彼得111我很高兴它有帮助: