Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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 在检索到的数组上执行for循环_Java_List_For Loop - Fatal编程技术网

Java 在检索到的数组上执行for循环

Java 在检索到的数组上执行for循环,java,list,for-loop,Java,List,For Loop,继续尝试一个应用程序,我又被下一个对象卡住了 上次我问我的Performer.java对象,它接收5个整数的数组 这次我试图通过我的计算对象来操作数据 (如果我贴错了标签,请纠正我) import java.util.List; 公共类计算{ 公共静态int-performCalcs(){ 执行者getInput=新执行者(); List arrayOne=getInput.getUnit(); 对于(int i=0;i而言,最好的方法是对每个循环使用: int sumTotal = 0; f

继续尝试一个应用程序,我又被下一个对象卡住了

上次我问我的Performer.java对象,它接收5个整数的数组

这次我试图通过我的计算对象来操作数据

(如果我贴错了标签,请纠正我)

import java.util.List;
公共类计算{
公共静态int-performCalcs(){
执行者getInput=新执行者();
List arrayOne=getInput.getUnit();

对于(int i=0;i而言,最好的方法是对每个循环使用:

int sumTotal = 0;

for(Integer val: arrayOne) {
    sumTotal += val.intValue();
}

最好的方法是为每个循环使用:

int sumTotal = 0;

for(Integer val: arrayOne) {
    sumTotal += val.intValue();
}

注意sumTotal是如何在循环中返回的,这意味着它在第一次通过循环时返回第一个值

import java.util.List;


public class Calculations {

public static int performCalcs(){


    Performer getInput = new Performer();

    List<Integer> arrayOne = getInput.getUnit();

    int sumTotal = 0;
    for(int i=0 ; i<arrayOne.size() ; i++){
        sumTotal+=arrayOne.get(i);
    }
    return sumTotal;
}

}
返回两个,然后使用找到的第一个

编辑: 正如Maroun所正确指出的,for循环应该是

for(int i=0 ; i<arrayOne.size() ; i++){

for(int i=0;i注意sumTotal是如何在循环中返回的,这意味着在第一次通过循环时它返回第一个值

import java.util.List;


public class Calculations {

public static int performCalcs(){


    Performer getInput = new Performer();

    List<Integer> arrayOne = getInput.getUnit();

    int sumTotal = 0;
    for(int i=0 ; i<arrayOne.size() ; i++){
        sumTotal+=arrayOne.get(i);
    }
    return sumTotal;
}

}
返回两个,然后使用找到的第一个

编辑: 正如Maroun所正确指出的,for循环应该是

for(int i=0 ; i<arrayOne.size() ; i++){
for(int i=0;i
  • 您应该将
    sumTotal
    移动到for循环的外部。否则每次都会重新初始化
    sumTotal
    变量
  • 您应该循环到
    i
    .O.w。您将获得IndexOutOfBoundsException
  • 您可以通过以下方式检索第i个数字:
    arrayOne.get(i)
  • 然后将
    sumTotal
    除以
    arrayOne
    列表中的值数,计算
    平均值
代码如下:

import java.util.List;

public class Calculations {

    public static int performCalcs() {

        Performer getInput = new Performer();

        List<Integer> arrayOne = getInput.getUnit();

        int sumTotal = 0;

        // Sum the numbers in the array 
        // Repeat until i < arrayOne.size()
        for (int i = 0; i < arrayOne.size(); i++) {
            int num = arrayOne.get(i);
            sumTotal += num;
        }
        // Check that the array is not empty.
        // If it is not, compute the average, o.w. return 0.
        double avg = arrayOne.isEmpty() ? .0 : (sumTotal / arrayOne.size());
        System.out.println("average: " + avg);
        return avg;
    }
}
import java.util.List;
公共类计算{
公共静态int-performCalcs(){
执行者getInput=新执行者();
List arrayOne=getInput.getUnit();
int-sumTotal=0;
//对数组中的数字求和
//重复,直到i
  • 您应该将
    sumTotal
    移动到for循环的外部。否则每次都会重新初始化
    sumTotal
    变量
  • 您应该循环到
    i
    .O.w。您将获得IndexOutOfBoundsException
  • 您可以通过以下方式检索第i个数字:
    arrayOne.get(i)
  • 然后将
    sumTotal
    除以
    arrayOne
    列表中的值数,计算
    平均值
代码如下:

import java.util.List;

public class Calculations {

    public static int performCalcs() {

        Performer getInput = new Performer();

        List<Integer> arrayOne = getInput.getUnit();

        int sumTotal = 0;

        // Sum the numbers in the array 
        // Repeat until i < arrayOne.size()
        for (int i = 0; i < arrayOne.size(); i++) {
            int num = arrayOne.get(i);
            sumTotal += num;
        }
        // Check that the array is not empty.
        // If it is not, compute the average, o.w. return 0.
        double avg = arrayOne.isEmpty() ? .0 : (sumTotal / arrayOne.size());
        System.out.println("average: " + avg);
        return avg;
    }
}
import java.util.List;
公共类计算{
公共静态int-performCalcs(){
执行者getInput=新执行者();
List arrayOne=getInput.getUnit();
int-sumTotal=0;
//对数组中的数字求和
//重复,直到i
要对任何
Iterable
中的所有数字求和,需要执行以下操作:

//Declare the variable to hold the total outside the loop.
int total = 0;

//Loop over the integers to sum.
for (Integer i : integers)
{
  //Add the current integer to the total.
  total += i;
}

//The total will now equal the sum of all the integers.
System.out.println("Total: " + total);
System.out.println("Average: " + (total / integers.size()));
代码缺少的两个重要位是:

  • total变量必须在循环外部声明,否则每次迭代都可以简单地重新初始化
  • 您需要将整数和值相加

要对任何
Iterable
中的所有数字求和,您需要执行以下操作:

//Declare the variable to hold the total outside the loop.
int total = 0;

//Loop over the integers to sum.
for (Integer i : integers)
{
  //Add the current integer to the total.
  total += i;
}

//The total will now equal the sum of all the integers.
System.out.println("Total: " + total);
System.out.println("Average: " + (total / integers.size()));
代码缺少的两个重要位是:

  • total变量必须在循环外部声明,否则每次迭代都可以简单地重新初始化
  • 您需要将整数和值相加

for(inti=0;i
for(inti=0;i我想应该是这样的

public class Calculations {
公共静态int-performCalcs(){

Performer getInput=new Performer();
List arrayOne=getInput.getUnit();

对于(inti=0;i我想应该是这样的

public class Calculations {
公共静态int-performCalcs(){

Performer getInput=new Performer();
List arrayOne=getInput.getUnit();

对于(inti=0;i您应该从循环中提取sum init和return

public static int performCalcs(){


    Performer getInput = new Performer();
    List<Integer> arrayOne = getInput.getUnit();
    int sumTotal = 0;

    for(int i=0 ; i < arrayOne.size(); i++){
        return sumTotal += arrayOne.get(i);
    }

    return sumTotal;
}
publicstaticintperformcalcs(){
执行者getInput=新执行者();
List arrayOne=getInput.getUnit();
int-sumTotal=0;
对于(int i=0;i
您应该从循环中提取sum init和return

public static int performCalcs(){


    Performer getInput = new Performer();
    List<Integer> arrayOne = getInput.getUnit();
    int sumTotal = 0;

    for(int i=0 ; i < arrayOne.size(); i++){
        return sumTotal += arrayOne.get(i);
    }

    return sumTotal;
}
publicstaticintperformcalcs(){
执行者getInput=新执行者();
List arrayOne=getInput.getUnit();
int-sumTotal=0;
对于(int i=0;i
这里的问题是,您在循环内部使用“return”。i++部分很好。在循环外部使用return。以上所有答案都显示了这一点


如果在循环中使用“return”,它只会迭代一次。

这里的问题是在循环中使用“return”。i++部分很好。在循环之外使用return。上面的所有答案都显示了这一点


如果在循环中使用“return”,它将只迭代一次。

int-sumTotal=0;
移出循环(如上)并
返回。。。