找到Java中ArrayList的连续子数组

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

Finding continuous subarray in ArrayList java

问题

我是新手学习Java并且正在练习我正在编写一个程序用于在给定整数数组中找到所有连续的子数组为了简单起见我通过键盘插入输入每个数字在新行中),数组的结束指示是一个负整数

我使用以下方法填充数组

```java
public static void main(String []args){
		// 分配新数组       
        ArrayList<Integer> inputArray = new ArrayList<Integer>(); 
        
        int number = 0;
        while(number >= 0) {
			
			// 从用户获取输入
			Scanner input = new Scanner(System.in);
			
			number = input.nextInt();
			
            inputArray.add(number); 	
        }

        // 对数组进行排序
        Collections.sort(inputArray);

       // 删除负整数(存储在数组的第一个单元格中)
       inputArray.remove(0);

        // 分配新数组以存储序列
        ArrayList<Integer> sequence = new ArrayList<Integer>();
		
		// 获取第一个群集
		sequence = FindSequence(0, inputArray);

		for(int it : sequence){
			System.out.println(it);

		}
}

直到这里,一切都按预期工作。

最后,我正在尝试使用此方法找到第一个连续整数序列,这不完全是我的目标,但我从更简单的东西开始,然后我会进一步使用这个方法来找到所有连续序列:

public static ArrayList<Integer> FindSequence(int index, ArrayList<Integer> input) {
	ArrayList<Integer> sequence = new ArrayList<Integer>();

	while(input.get(index) == input.get(index + 1) || input.get(index) == input.get(index + 1) + 1 ) {
		sequence.add(input.get(index));
		index++;
	}
	
	sequence.add(input.get(index));

    for(int it : sequence) {
			System.out.println(it);
	}
	
	return sequence;
}

这两个方法在同一个类中声明和实现。

问题是,我输入的序列是1 2 3 -1,我期望打印出1 2 3,但我得到的输出只有1
我尝试过使用打印进行调试,我发现程序不进入while循环,尽管条件已经满足,因为input.get(index) = 1,而input.get(index + 1) = 2,所以input.get(index) == input.get(index + 1) + 1为真。

运算符||的意思(据我所知)是布尔或,因此只有其中一个条件必须满足,程序才能进入while循环。我真的很困惑,不知道为什么会发生这种情况,也许有人可以解释并建议我如何解决这个问题?

谢谢。


<details>
<summary>英文:</summary>

I&#39;m new to java, and I&#39;m practicing. I&#39;m writing a program to find all continuous subarrays in a given array of integers. For simplicity, I insert the input from the keyboard (each number in new line) and the indication for the end of the array is a negative integer.

I fill the array using this method:

public static void main(String []args){
// allocate new array
ArrayList<Integer> inputArray = new ArrayList<Integer>();

    int number = 0;
    while(number &gt;= 0) {
		
		// get input from user
		Scanner input = new Scanner(System.in);
		
		number = input.nextInt();
		
        inputArray.add(number); 	
    }

    // sort the array
    Collections.sort(inputArray);

   // remove the negative integer (which is stored in the first cell of the array)
   inputArray.remove(0);

    // allocate new array to store the sequence
    ArrayList&lt;Integer&gt; sequence = new ArrayList&lt;Integer&gt;();
	
	// get first cluster
	sequence = FindSequence(0, input);

	for(int it : sequence){
		System.out.println(it);

	}

}


Up until here, everything works as expected.

At last, I&#39;m trying to use this method to find the first sequence of continuous integers, it&#39;s not exactly my goal, but I&#39;m starting from something more simple and then I&#39;ll progress and use this method to find all continuous sequences:

public static ArrayList<Integer> FindSequence(int index, ArrayList<Integer> input) {
ArrayList<Integer> sequence = new ArrayList<Integer>();

while(input.get(index) == input.get(index + 1) || input.get(index) == input.get(index + 1) + 1 ) {
	sequence.add(input.get(index));
	index++;
}

sequence.add(input.get(index));

for(int it : sequence) {
		System.out.println(it);
}

return sequence;

}


Both methods are declared and implemented in the same class. 

The problem is that the sequence I enter is `1 2 3 -1` I expect ` 1 2 3` to be the printed but all that I get as output is `1`.
I&#39;ve tried debugging a bit using prints - and I discovered that the program doesn&#39;t enter the while loop, although the condition is satisfied, since `input.get(index) = 1` and `input.get(index + 1) = 2` so `input.get(index) == input.get(index + 1) + 1`  is true. 

The operator `||` means (as far as I know) boolean or, so only one of the conditions must be satisfied in order for the program to enter the while loop. I&#39;m really confused, and I have no idea why this is happening, perhaps someone could explain and advice me on how to fix this?

Thank you.

</details>


# 答案1
**得分**: 1

逻辑有些错误,因为下一个元素应该等于前一个元素 + 1
应该是这样的:

```java
while (input.get(index) == input.get(index + 1) || (input.get(index) + 1) == input.get(index + 1))
英文:

The logic is a bit wrong, since the next element should be equal to the predecessor + 1
It should look like this :

while(input.get(index) == input.get(index + 1) || (input.get(index) + 1) == input.get(index + 1))

答案2

得分: 1

在线input.get(index) == input.get(index + 1) + 1中,您想要检查下一个索引是否比当前索引大1。

为了进行相同的检查,在将1添加到当前元素后,它应该等于下一个元素。相同检查的布尔语句可以是

input.get(index) + 1 == input.get(index + 1)
英文:

In line input.get(index) == input.get(index + 1) + 1 you want to check that the next index is 1 greater than the current one.

To check the same, after adding 1 to the current element it should equal the next one. The boolean statement for the same can be

input.get(index) + 1 == input.get(index + 1)

答案3

得分: 1

下面的代码可以正常工作。

import java.io.*;
import java.util.*;

class GFG {
    public static void main(String[] args) {
        ArrayList<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(-1);
        FindSequence(0, list);
    }

    public static ArrayList<Integer> FindSequence(int index, ArrayList<Integer> input) {
        ArrayList<Integer> sequence = new ArrayList<Integer>();

        while (input.get(index) == input.get(index + 1) || input.get(index) + 1 == input.get(index + 1)) {
            sequence.add(input.get(index));
            index++;
        }

        sequence.add(input.get(index));

        for (int it : sequence) {
            System.out.println(it);
        }
        return sequence;
    }
}
英文:

It's the condition check that you are using, that violates the entire purpose of finding the continuous series. The below code works fine.

import java.io.*;
import java.util.*;

class GFG {
 public static void main (String[] args) {
	ArrayList&lt;Integer&gt; list = new ArrayList&lt;&gt;();
	list.add(1);
	list.add(2);
	list.add(3);
	list.add(-1);
	FindSequence(0,list);
 }

 public static ArrayList&lt;Integer&gt; FindSequence(int index, ArrayList&lt;Integer&gt; input) {
  ArrayList&lt;Integer&gt; sequence = new ArrayList&lt;Integer&gt;();

  while(input.get(index) == input.get(index + 1) || input.get(index) + 1 == input.get(index + 1)) {
    sequence.add(input.get(index));
    index++;
 }

  sequence.add(input.get(index));

  for(int it : sequence) {
        System.out.println(it);
  }
  return sequence;
 }
}

huangapple
  • 本文由 发表于 2020年9月12日 17:41:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/63858943.html
匿名

发表评论

匿名网友

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

确定