【问题标题】:Sum of elements in an array数组中元素的总和
【发布时间】:2015-09-20 17:09:00
【问题描述】:

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

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

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.");
    }
}

【问题讨论】:

  • 为了将来参考,这类问题更适合 codereview.stackexchange.com 。
  • 这个问题和这个类似吗? stackoverflow.com/questions/4550662/…
  • 你应该缩进你的main
  • 哦,我以为 main 是缩进的。好的,谢谢。

标签: java arrays for-loop foreach traversal


【解决方案1】:

不要修改数组。我更喜欢for-each loop。你应该考虑到可能有很多学生,所以我可能会使用long 来代替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);

【讨论】:

  • 老实说,我尝试过类似的方法,但它似乎只是将索引相加,所以我搞砸了它,直到我得到上面的内容。我会再试一次,因为我刚刚查看了分配,它需要一个额外的循环来打印出无法按照我的方式发生的元素,因为我改变了我猜的数组。
  • 我可能会使用 long long,因为我认为 int 和 long 在某些平台上可能具有相同的大小
  • @amuse 不在 Java 中,并且 Java 没有 long long。另请参阅JLS-4.2. Primitive Types and Values
  • 哦,不要注意语言(一开始我认为答案与语言无关),但我怀疑它是否需要使用 long,因为我认为 long 喜欢一个陷阱,如果一些可能会导致溢出其他程序员不注意 sum 的类型,例如,如果我想使用 sum 进行其他计算,我可能会有类似:int xxx=suma/b 而不是 long xxx=suma /b.
【解决方案2】:
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.");
    }
}

【讨论】:

    【解决方案3】:

    在 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.");
        }
    }
    

    【讨论】:

      【解决方案4】:

      我知道的最短方法是:

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

      【讨论】:

        【解决方案5】:

        除了其他不错的贡献之外,我是for-each loop 的粉丝,通常会在一行中完成。

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

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

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

        如果我需要在for loopfor-each loop 中包含多行代码,那么我会将它们全部放在大括号{ more than one line of code } 中。

        【讨论】:

          猜你喜欢
          • 2016-07-22
          • 1970-01-01
          • 1970-01-01
          • 2017-07-02
          • 1970-01-01
          • 2017-12-06
          • 1970-01-01
          • 2019-04-21
          • 2012-10-17
          相关资源
          最近更新 更多