为简单起见,假设我有一个6 4 5 6 0 6 3 4 1 6 1 6 0 6 8 3
,其索引只包含一个单位数整数。例如:
6 0 6
我想过滤掉所有出现的子列表6 4 5 3 4 1 6 1 8 3
,以便新列表成为:
ListIterator
有没有办法做到这一点?使用public static void filterList(ArrayList<Integer> list) {
ListIterator<Integer> iterator = list.listIterator();
int elem;
while (iterator.hasNext()) {
// Remove any sublist of 6 0 6
}
}
对我来说似乎不起作用,因为我必须集体考虑三个连续元素,而且老实说我不确定如何做到这一点。
这是我实施的方法的骨架:
$.ajax({
type: "POST",
url: "pgLoadFile.aspx/checkFilesProcess",
contentType: "application/json; charset=utf-8",
data: JSON.stringify(someData),
dataType: "json",
success: function (data) {
if (data.d != "" && data.d != undefined) {
if (data.d == "0") {
$("#showCheckFileFailMessage").show("slow");
}
else {
$("#showCheckFileSuccessMessage").show("slow");
$("#trLoadFile").show("slow");
}
}
else $("#showCheckFileFailMessage").show("slow");
},
error: function (ts) {
alert(ts.responseText)
$("#showCheckFileFailMessage").show("slow");
},
timeout: 60000
});
编辑:再次,为简单起见,我们假设不存在60606或类似情况。
答案 0 :(得分:2)
您可以使用Collections.indexOfSubList
:
public static void removeAllSubList(List<?> list, List<?> subList) {
// find first occurrence of the subList in the list, O(nm)
int i = Collections.indexOfSubList(list, subList);
// if found
if (i != -1) {
// bulk remove, O(m)
list.subList(i, i + subList.size()).clear();
// recurse with the rest of the list
removeAllSubList(list.subList(i, list.size()), subList);
}
}
答案 1 :(得分:2)
[编辑 - 更好,单程方法]
自定义,增强型indexOfSublist
从offset
开始搜索;因此,每次我们删除某些内容时,我们都不会从0
重新启动(正如我们在使用Collections.indexOfSublist
时所做的那样,请参阅此答案的底部)。
static <T> int indexOfSublist(List<T> haystack, List<T> needle, int offset){
int toRet=-1;
int needleLen=needle.size();
if(needleLen>0) {
// it makes sense to search
int haystackLen=haystack.size();
for(;offset+needleLen<haystackLen && toRet<0; offset++) {
int compIx;
for(
compIx=0;
(
compIx<needleLen
&& false==haystack.get(offset+compIx).equals(needle.get(compIx))
);
compIx++
);
if(compIx==needleLen) { // found
toRet=offset;
}
}
}
return toRet;
}
public static void filterList(List<Integer> haystack, List<Integer> needle) {
for(
int offset=0, ixOfNeedle=indexOfSublist(haystack, needle, offset);
ixOfNeedle>=0;
ixOfNeedle=indexOfSublist(haystack, needle, offset)
) {
// found one place. We'll continue searching from here next time
offset=ixOfNeedle;
//////////////////////////////////////////
// for a better removal sequence, see the
// 4castle's answer using sublists
for(int i=needle.size(); i>0; i--) {
haystack.remove(ixOfNeedle);
}
}
}
Collections.indexOfSublist就是你所追求的。
public static void filterList(ArrayList<Integer> haystack, List<Integer> needle) {
for(
int ixOfNeedle=Collections.indexOfSublist(haystack, needle);
ixOfNeedle>=0;
ixOfNeedle=Collections.indexOfSublist(haystack, needle)
) {
for(int i=needle.size(); i>0; i--) {
haystack.remove(ixOfNeedle);
}
}
}
答案 2 :(得分:1)
我建议您在ArrayList
之前搜索ListIterator
。
public static void filterList(ArrayList<Integer> list) {
bool firstInstance = false; //Saying we having found our first instance of our sub list
for(int i=0;i<list.size();++i) {
if(list.get(i) == 6) //Checks to see if our first index is a 6 or it pointless to check the next two numbers i.e. wasting resources
if(list.get(i+1) == 0 && list.get(i+2) == 6 && !firstInstance) { //Make sure it a 6 0 6 list
list.remove(i); //Removes first one
list.remove(i); //Removes second one which now became our current index number
list.remove(i); //Removes third one which now became our current index number
} else
firstInstance = true; //Our first instances has been found and will now remove duplicate ones!
}
ListIterator<Integer> iterator = list.listIterator();
int elem;
while (iterator.hasNext()) {
// Remove any sublist of 6 0 6-- Already Done
}
}
答案 3 :(得分:0)
您可以使用数组和列表组合作为查找以下解决方案,希望它能为您提供帮助。
public void testData()
{
int tempArray[] = {6, 4, 5, 6, 0, 6, 3, 4, 1, 6, 1, 6, 0, 6, 8, 3};
List<Integer> resultArray = new ArrayList<>();
for(int i = 0; i < tempArray.length; i++)
{
if(tempArray[i] == 6 && tempArray[i+1] == 0 && tempArray[i + 2] == 6)
{
i += 2;
}
else
{
resultArray.add(tempArray[i]);
}
}
for(int tempInt : resultArray)
{
System.out.print("\t" + tempInt);
}
}
注意:您可以根据上述功能的要求从您身边传递数据或返回结果。