大家好我想问一些问题,如果我想检查数组是否有4个连续的元素,或者不是这个{1,2,4,4,4,4,3,5}
它会返回true
但是如果数组是{1,2,4,4,4,2,4,2}
返回false
请帮助Java :)}这是我的代码:
public class arraysearch {
public static void main(String[] args) {
// TODO Auto-generated method stub
int arr [] ={1,2,4,4,4,4,5,7,10};
int start = arr[0];
int count = 0;
int sum=0;
for(int i = 1;i<arr.length-1;i++)
{
if(start==4)
{
count++;
}
else
{
//count=0;
for(int j=i+1;j<arr.length-1;j++){
if(arr[i]==arr[j]&&arr[j]==4)
{
count++;
//continue;
}
}
}
}
if(count == 4)
{
System.out.println("it has 4 elements");
}
else
System.out.println("it hasn't 4 elements");
}
}
答案 0 :(得分:3)
一个独特的解决方案,供参考:
int arr [] ={1,2,4,4,4,4,5,7,10};
for(int o : arr)
buffer.append(",").append(o);
boolean match = buffer.toString().matches("(\\d+)(?:,\\1){3}");
System.out.println("It "+(match ? "has" : "hasn't")+ " 4 elements");
您可以重复使用该模式以提高效率:
// Class
private static final Pattern CHAIN=Pattern.compile("(\\d+)(?:,\\1){3}");
// Method
int arr [] ={1,2,4,4,4,4,5,7,10};
for(int o : arr)
buffer.append(",").append(o);
System.out.println("It "+(CHAIN.matcher(buffer).matches() ? "has" : "hasn't")+ " 4 elements");
让我在这里解释一下这个魔法:
(\d+)(?:,\1){3}
是正则表达式。当一行数字连续四次出现时,它与逗号分开。以下是分解后的样子:
> Capturing Group "\d+" one or more number digits
| > Non- Capturing Group: ",\1" a comma and a captured group
| | > The second group matches 3 times
| | |
(\d+)(?:,\1){3}
答案 1 :(得分:1)
您的代码不会区分连续元素和分隔元素。你可以做什么(略微修改)重置计数。
count = 1;
for(int i=0; i<arr.size-1; i++){
if(arr[i+1] == arr[i]){
count++;
}
else{
count=1;
}
if(count == 4){
return true;
}
}
此代码通过检查下一个元素是否与当前元素相同来迭代,如果是,则向计数添加一个元素。如果不是,则重置下一个链的计数。这适用于任何一系列数字,并且总是存储最长的数字,所以我建议检查它是否包含一系列至少4个元素。
答案 2 :(得分:0)
试试这个:
public class SumDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
int arr[] = {
1, 2, 4, 4, 4, 4, 5, 7, 10
};
int count = 0;
for (int i = 0; i < arr.length - 1; i++) {
if (arr[i] != arr[i + 1])
continue;
else
count++;
}
if (count >= 3) { // three comparision found true, means the array has 4
// simliar continuous elements
System.out.println("it has " + count + "elements");
} else
System.out.println("it has " + count + "elements");
}
}
如果该解决方案具有多个连续的元素,则该解决方案将无效。例如{1,2,2,4,4,4,4,5,7,10}
答案 3 :(得分:0)
好吧,你的代码似乎有些搞砸了。您询问您的代码是否有4个连续的元素,但在您的代码中,您似乎正在测试它是否连续4个4
。假设您的数组至少包含1个元素:
int[] arr = new int[]{1, 2, 4, 4, 4, 2, 4, 2};
//current consecutive sector
int currentLength = 1;
int currentValue = arr[0];
//longest consecutive sector so far
int maxLength = 1;
int maxValue = currentValue;
for (int i = 1; i < arr.length; i++) {
if (currentValue == arr[i]) {
//hooray, current consecutive sector is getting larger
currentLength++;
} else {
//oh no, current consecutive sector ends and starts a new one
if (currentLength > maxLength) {
maxLength = currentLength;
maxValue = currentValue;
}
currentLength = 1;
currentValue = arr[i];
}
}
//finally, check if the last sector isn't the longest
if (currentLength > maxLength) {
maxLength = currentLength;
maxValue = currentValue;
}
System.out.format("Longest consecutive sector contained %d times number %d.\n",
maxLength, maxValue);