如何根据分隔符将数组列表分割成两个数组列表?

huangapple go评论123阅读模式
英文:

How do I split an array list into two array lists based on a delimiter?

问题

假设我有一个数组列表的数组列表:

A = [2, 5, 6], [1, 6, 1], [], [7, 7], [4, 2], [9, 3]]

我想在其中数组列表的元素大小为0的索引处将它们分割,使其看起来像这样:

x = [[2, 5, 6], [1, 6, 1]]
y = [[7, 7], [4, 2], [9, 3]]

但无论我运行循环如何,结果都是这样的:

for(int i = 0; i < A.size(); i++) {
	ArrayList temp= A.get(i);
	int size = temp.size();
	if(size != 0) {
	   x.add(temp);
	}
	   y.add(temp);
	}
x = [[2, 5, 6], [1, 6, 1], [7, 7], [4, 2], [9, 3]]
y = [[2, 5, 6], [1, 6, 1], [], [7, 7], [4, 2], [9, 3]]

有人可以帮我解决这个问题吗?

英文:

Suppose that I have an Array list of Array lists:

A = [2, 5, 6], [1, 6, 1], [], [7, 7], [4, 2], [9, 3]]

and I want to split them at the index where the element of the array list has the size of 0 so that it will look like this

x = [[2, 5, 6], [1, 6, 1]]
y = [[7, 7], [4, 2], [9, 3]]

but whenever I run my loop it comes out like this:

for(int i = 0; i < A.size(); i++) {
	ArrayList temp= A.get(i);
	int size = temp.size();
	if(size != 0) {
	   x.add(temp);
	}
	   y.add(temp);
	}
x = [[2, 5, 6], [1, 6, 1], [7, 7], [4, 2], [9, 3]]
y = [[2, 5, 6], [1, 6, 1], [], [7, 7], [4, 2], [9, 3]]

Can anyone please help me figure this out?

答案1

得分: 1

你的逻辑有问题。目前,你避免将空列表写入x;除此之外,你总是同时向xy写入数据。

你可以使用一个布尔值来指示数据必须写入哪个列表。一旦你看到一个大小为0的列表,就切换这个标志。

boolean writeToX = true;

for (int i = 0; i < A.size(); i++) {
    ArrayList temp = A.get(i);
    int size = temp.size();
    if (size == 0) {
        writeToX = false;
        continue;
    }
    if (writeToX) {
        x.add(temp);
    } else {
        y.add(temp);
    }
}

附注:避免使用原始类型。

https://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-we-use-it

英文:

You logic is off. Currently you are avoiding writing the empty list into x; other than that you always write to both x and y.

You can have a boolean to say which list the data must be written to. Once you see a list with size 0, switch the flag.

boolean writeToX = true;

for(int i = 0; i &lt; A.size(); i++) {
    ArrayList temp= A.get(i);
    int size = temp.size();
    if(size == 0) {
       writeToX = false;
       continue;
    }
    if (writeToX) {
        x.add(temp);
    } else {
        y.add(temp);
    }
      
}

Sidenote: Avoid using raw types.

https://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-we-use-it

答案2

得分: 1

你需要像这样跟踪目标数组:

boolean addToX = true;

for(ArrayList temp : A) {
  if(temp.isEmpty()) {
    addToX = false;  
  } else if(addToX) {
    x.add(temp);
  } else {
    y.add(temp);
  }
}
英文:

You need to keep track of the destination array like this:

boolean addToX = true;

for(ArrayList temp : A) {
  if(temp.isEmpty()) {
    addToX = false;  
  } else if(addToX) {
    x.add(temp);
  } else {
    y.add(temp);
  }
}

答案3

得分: 0

尝试这个,在代码中添加一个布尔标志来标识:

boolean flag = false;
for (int i = 0; i < A.size(); i++) {
    ArrayList temp = A.get(i);
    int size = temp.size();
    if (size != 0 && flag == false) {
        x.add(temp);
    } else if (size == 0) {
        flag = true;
    } else if (size != 0 && flag == true) {
        y.add(temp);
    }
}
英文:

Try this, add a boolen flag to identify :-

	boolean flag=false;
	for(int i = 0; i &lt; A.size(); i++) {
	    ArrayList temp= A.get(i);
	    int size = temp.size();
	    if(size != 0 &amp;&amp; flag==false) {
	       x.add(temp);
	    }else if(size==0) {
	       flag=true;
	    }else if(size != 0 &amp;&amp; flag==true){
	    	y.add(temp);
	    }

答案4

得分: 0

这是正常的,因为你只是没有将空元素放入 x,但你将所有其他元素都放入了 x 和 y。你可以有一个名为 isNullArrayCome 的布尔参数,在每次迭代时检查它。这里是代码示例:

boolean isNullArrayCome = false;
for (int i = 0; i < A.size(); i++) {
    ArrayList temp = A.get(i);
    int size = temp.size();
    if (size == 0) {
        isNullArrayCome = true;
    }
    if (!isNullArrayCome) {
        x.add(temp);
    } else {
        y.add(temp);
    }
}
英文:

it is normal because you are just not putting the empty element to x but you are putting all other elements to x and y. You can have a boolean parameter, such as isNullArrayCome and check it on every iteration. Here it is

boolean isNullArrayCome = false;
for(int i = 0; i &lt; A.size(); i++) {
    ArrayList temp= A.get(i);
    int size = temp.size();
    if(size == 0 ) {
      isNullArrayCome = true;
     }
     if(!isNullArrayCome){
       x.add(temp);
     } else {
       y.add(temp);
     }
}

huangapple
  • 本文由 发表于 2020年10月23日 16:07:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/64496250.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定