java 数组中元素的总和

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31196967/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 18:15:29  来源:igfitidea点击:

Sum of elements in an array

javaarraysfor-loopforeachtraversal

提问by NoobCoderChick

I'm working on a simple assignment for a summer java course and was just hoping you guys could take a look at my code and see if the way I did it is the best way. The purpose is to create a simple intarray with at least 25 elements and use a loop to traverse it and add up all the elements. I had some issues but looks like I got it to work. After I work it out I did a little research and saw some similar stuff where people were using a For Each loop (enhanced loop). Would that be a better option? I'm kinda confused on the best ways to use that opposed to a regular for loop.

我正在为暑期 Java 课程做一个简单的作业,只是希望你们能看看我的代码,看看我的方法是否是最好的方法。目的是创建一个int至少包含 25 个元素的简单数组,并使用循环遍历它并将所有元素相加。我遇到了一些问题,但看起来我已经开始工作了。在我解决之后,我做了一些研究,看到了一些类似的东西,人们在使用 For Each 循环(增强循环)。那会是一个更好的选择吗?我对使用与常规 for 循环相反的最佳方法感到困惑。

Anyway, any comments or criticism in helping me be a better programmer!

无论如何,任何有助于我成为更好程序员的评论或批评!

public class Traversals {

    public static void main(String[] args) {

        int absenceTotal = 0;
        // initialize array with 30 days of absences.
        int absencesArr[] = { 1, 3, 0, 9, 8, 23, 1, 
                11, 23, 5, 6, 7, 10, 1, 5,
                14, 2, 4, 0, 0, 1, 3, 2, 1, 
                1, 0, 0, 1, 3, 7, 2 };

        for (int i = 0; i < absencesArr.length; i++) {
            absencesArr[i] += absenceTotal;
            absenceTotal = absencesArr[i];
        }
        System.out.println("There were " + absenceTotal + " absences that day.");
    }
}

回答by Elliott Frisch

Don't modify the array. I would prefer the for-eachloop. And you should consider that there may be a very large number of students, so I would probably use a longfor sum. And formatted output. Putting that together into something like

不要修改数组。我更喜欢for-each循环。而且您应该考虑到可能有非常多的学生,所以我可能会使用longfor sum。并格式化输出。把它放在一起变成类似的东西

long sum = 0;
for(int i : absencesArr) {       
    sum += i;
}   
// System.out.println("There were " + sum + " absences that day.");   
System.out.printf("There were %d absences that day.%n", sum);

回答by Josua Marcel Chrisano

public class Traversals {

    public static void main(String[] args) {

        int absenceTotal = 0;
        // initialize array with 30 days of absences.
        int absencesArr[] = { 1, 3, 0, 9, 8, 23, 1, 
                11, 23, 5, 6, 7, 10, 1, 5,
                14, 2, 4, 0, 0, 1, 3, 2, 1, 
                1, 0, 0, 1, 3, 7, 2 };

        for (int i = 0; i < absencesArr.length; i++) {
            // remove this
            //absencesArr[i] += absenceTotal;
            absenceTotal += absencesArr[i]; //add this
        }
        System.out.println("There were " + absenceTotal + " absences that day.");
    }
}

回答by fetta

In Java 8 you can use stream api:

在 Java 8 中,您可以使用流 api:

public class Traversals {
    public static void main(String[] args) {

        int absenceTotal = 0;
        // initialize array with 30 days of absences.

        int absencesArr[] = { 1, 3, 0, 9, 8, 23, 1, 
                11, 23, 5, 6, 7, 10, 1, 5,
                14, 2, 4, 0, 0, 1, 3, 2, 1, 
                1, 0, 0, 1, 3, 7, 2 };

        absenceTotal = IntStream.of(array).sum();

        System.out.println("There were " + absenceTotal + " absences that day.");
    }
}

回答by sushmitha shenoy

shortest way i know would be :

我知道的最短方法是:

int sum=Arrays.stream(absencesArr).sum();

回答by maytham-???????

In addition to other nice contributions, I am fan of for-each loopand will typically do it in one line.

除了其他不错的贡献之外,我很for-each loop喜欢并且通常会在一行中完成。

for(int i : absencesArr) absenceTotal += i;
System.out.printf("There were %d absences that day.", absenceTotal);

But in some situations when I want to have control over my Object size/length/count, I will use for looplike following example:

但是在某些情况下,当我想控制对象大小/长度/计数时,我将使用for loop如下示例:

for (int i = 0; i < absencesArr.length; i++) absenceTotal += absencesArr[i];
System.out.printf("There were %d absences that day.", absenceTotal);

And if I need to have more then one line of codes inside the for loopor for-each loopthen I will put them all inside curly brackets { more than one line of code }.

如果我需要有更多的,然后里面的代码的一行for loopfor-each loop然后我会把他们都内大括号{ more than one line of code }