Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从不同的位置开始遍历数组_Java_Arrays - Fatal编程技术网

Java 从不同的位置开始遍历数组

Java 从不同的位置开始遍历数组,java,arrays,Java,Arrays,我正在尝试通过一个数组: public static String calculate(int [] bills,int amount){ int size = bills.length; int cache = 0; for (int i = 0; i < size; i++) { cache+=bills[i]; if(cache==amount){ return "OK"; } } return "NO OK"; } PSS:

我正在尝试通过一个数组:

public static String calculate(int [] bills,int amount){
int size = bills.length;        
int cache = 0;
for (int i = 0; i < size; i++) {
    cache+=bills[i];
    if(cache==amount){
        return "OK";
    }
}
return "NO OK";
}
PSS:这是我尝试过的代码(使用双循环,我犯的错误不是较少的,因为我删除了它,并且尝试了很多新的东西,修改了很多,这一个可能有可怕的错误……):

公共静态字符串计算(int[]票据,int金额){
int size=bills.length;
int cache=0;
int c=1;
int z=尺寸-1;
int s=0;
int w=0;
布尔时间=假;
对于(int i=w;i0&&j==z&&TIME){
尺寸=i;
i=0;
时间=假;
}
}
w++;
尺寸=z+1;
cache=0;
时间=真;
}
}

下面是一个简单的逻辑。您可以添加一个新数组,然后在循环使用账单数组时,可以将索引加上上上一个索引的总和添加到新数组中,打印数组,然后对账单数组的编号重新排序并重复循环

可以使用更改帐单数组中元素的顺序。让我们假设你有

bills > [2, 5, 7, 1] : sum > [2, 7, 14, 15]
现在您需要旋转帐单数组,因此下面将使用arrayCopy执行此操作

int firstElement = bills[0];
System.arraycopy(bills, 1, bills, 0, size-1);
bills[size-1] = firstElement;
在以上三行中,复制数组的第一个元素,然后使用arrayCopy分别将
票据[1]
票据[2]
票据[3]
复制到位于
票据[0]
票据[1]
票据[2]
位置的同一数组中(基本上删除对第一个元素的备份,然后将所有元素向左移动)最后复制数组末尾的第一个元素时使用
bills[size-1]=firstElement
。有关arrayCopy如何工作的更多详细信息,请参见上文提供的javadoc链接

您将获得以下信息:

[2, 5, 7, 1] : [2, 7, 14, 15]
[5, 7, 1, 2] : [5, 12, 13, 15]
[7, 1, 2, 5] : [7, 8, 10, 15]
[1, 2, 5, 7] : [1, 3, 8, 15]
这是代码示例

导入java.util.array

public class SumArray {
    public static void main(String[] args) {
        int[] bills = new int[] { 2, 5, 7, 1 };
        int size = bills.length;

        for (int i = 0; i < size; i++) {
            int[] sum = new int[size];
            for (int j = 0; j < size; j++) {
                if (j == 0) {
                    sum[j] = bills[j];
                } else {
                    sum[j] += sum[j - 1] + bills[j];
                }
            }

            System.out.println(Arrays.toString(bills) + " : " + Arrays.toString(sum));

            int firstElement = bills[0];
            System.arraycopy(bills, 1, bills, 0, size-1);
            bills[size-1] = firstElement;
        }
    }
}
公共类数组{
公共静态void main(字符串[]args){
int[]bills=新的int[]{2,5,7,1};
int size=bills.length;
对于(int i=0;i
假设
开始
是您希望开始的项目的索引,这应该可以做到:

cache += bills[(i+start)%size];

这是基于斯科特·亨特的回答。试一试

for (int start = 0; start < size; start++) {
    for (int i = 0; i < size; i++) {
        cache += bills[start + i];
        if (cache == amount) {
            // You probably want to keep track of indices added to cache...
            return "OK";
        } else if (cache > amount) {
            cache = 0;
            // this break breaks out of the "i" loop, but continues the "start" loop
            break;
        }
    }
}
return "NO OK";
for(int start=0;start数量){
cache=0;
//此中断中断“i”循环,但继续“开始”循环
打破
}
}
}
返回“NO OK”;

为什么按什么顺序添加数字很重要?请说明您“尝试”了什么,以及它与您想要的有什么不同。完成,谢谢。我想尝试一些可能性,但不使用4整数数组的重现性,蛮力的kkind tho。。。我还有其他的尝试或计算方法,但这是唯一剩下的方法。谢谢你的帮助,谢谢你的提问。我不认为这是问题所在。谢谢,我宁愿用这一个来获取系列,但正如Scott所说,问题是如何将数组(4次)相加,然后通常从0开始,然后遍历数组,但从索引[1]开始当它达到[3]时,继续为0。等等:)@ScottHunter谢谢你指出,我遗漏了问题的一部分。我更新了我的答案,至少给出了这个系列。谢谢你!这很有效;),我将深入研究arraycopy的语法;)我还将测试Scott的。。向上投票@诺曼·卡里佐不确定你是否能胜出,因为你的声望很低,无论如何,你是受欢迎的,很高兴你的回答毕竟是有帮助的。感谢Scott用初始答案标记问题。是的,只需重复开始和i,从0到bills.length
cache += bills[(i+start)%size];
for (int start = 0; start < size; start++) {
    for (int i = 0; i < size; i++) {
        cache += bills[start + i];
        if (cache == amount) {
            // You probably want to keep track of indices added to cache...
            return "OK";
        } else if (cache > amount) {
            cache = 0;
            // this break breaks out of the "i" loop, but continues the "start" loop
            break;
        }
    }
}
return "NO OK";