数组之和;数组类列表

时间:2014-12-12 23:40:49

标签: java arrays for-loop arraylist

以下是你要求的Die Class ...... Alex你能否进一步解释在我的Die Class中使用.getValue?


public class Die
{
   private final int MAX = 6;  // maximum face value

   private int faceValue;  // current value showing on the die

   //-----------------------------------------------------------------
   //  Constructor: Sets the initial face value.
   //-----------------------------------------------------------------
   public Die()
   {
      faceValue = 1;
   }

   //-----------------------------------------------------------------
   //  Rolls the die and returns the result.
   //-----------------------------------------------------------------
   public int roll()
   {
      faceValue = (int)(Math.random() * MAX) + 1;

      return faceValue;
   }

   //-----------------------------------------------------------------
   //  Face value mutator.
   //-----------------------------------------------------------------
   public void setFaceValue (int value)
   {
      faceValue = value;
   }

   //-----------------------------------------------------------------
   //  Face value accessor.
   //-----------------------------------------------------------------
   public int getFaceValue()
   {
      return faceValue;
   }

   //-----------------------------------------------------------------
   //  Returns a string representation of this die.
   //-----------------------------------------------------------------
   public String toString()
   {
      String result = Integer.toString(faceValue);

      return result;
    }
}

我尝试使用不同的代码来查找我的Array和ArrayList的总和......这是我的代码。有人可以告诉我如何解决问题。

import java.util.ArrayList;


public class Driver {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        final int SIZE = 10;
        Die sumArray;
        Die sumArrayList;


        Die[] DieList = new Die[SIZE];

        ArrayList<Die> DieList2 = new ArrayList<Die>();

        for (int i = 0; i < SIZE; i++)
        {
            DieList[i] = new Die();
        }

        for (int i = 0; i < SIZE; i++)
        {
            DieList2.add(new Die());
        }

        for (int i = 0; i < SIZE; i++)
        {
            DieList[i].roll();
        }

        for (int i = 0; i < SIZE; i++)
        {
            DieList2.get(i).roll();
        }

        System.out.print("Array: "); 

        for (int i = 0; i < SIZE; i++)
        {
        System.out.print(DieList[i] + " ");
        }

        System.out.println();

        System.out.println("ArrayList: " + DieList2); //.get(index) specific index value in the arrayList

        for (Die i : sumArray){
            sumArray+= i;
        }

    //  sumArrayList = (DieList2(0) + DieList[1] + DieList[2] + DieList[3] + DieList[4] + DieList[5] + DieList[6] + DieList[7] + DieList[8] + DieList[9] + DieList[10]);

    }
}

2 个答案:

答案 0 :(得分:0)

我发现你很难理解一些概念:

  • Object类(所有类继承的类)中定义的print方法将打印对象的引用,而不是任何属性。这应该是显而易见的,因为print方法无法知道要打印的属性(属性列表和引用都不会存储在对象中逐个进行)。如果需要打印其内部属性,则需要专门编写一个方法来覆盖打印方法,但如果没有充分理由覆盖预定义方法,则不要这样做。称之为getValue()即

  • sumArray只是一个对象,而不是一个对象列表。迭代第一个for循环的方式是只读取一个对象,因为sumArray是您的Die对象之一,而不是列表。

  • 除了Strings之外,operator +仅用于基本类型(int,boolean,float,...)。如果+ =没有破坏你的编译,我理解编译器将操作解释为单个赋值操作,因此你说参考sumArray现在再次指向... sumArray。声明没用。

  • 至于评论的最后一行,由于上述原因,您无法对对象求和。对象是由许多属性和方法组成的复杂实体,并且编译器无法知道如何默认对这些实体求和。如果由于任何原因你从C ++到达Java,请注意你不能重载操作符(但肯定不是你的情况)

所以,如果你想要一个解决方案

  • 在Die类中定义getValue()方法以读取存储的值。我假设你已经正确编码了roll值以生成一个值并存储它。

总结数组中的所有值:

int sums1=0;    
for (Die i : DieList){
sums1+= i.getValue();
}

要汇总ArrayList中的所有值,请执行以下操作:

int sums2=0;    
for (int i=0;i<DieList2.size();i++){
     sums2+= DieList2.get(i).getValue();
}

最后打印出来:

System.out.println("Sum of array elements: "+sums1);
System.out.println("Sum of ArrayList elements: "+sums2);

答案 1 :(得分:0)

如果您只是想在掷骰子后找到数组的总和,那么您可以缩短代码!

你可以在一个循环中压缩所有骰子的添加,滚动和求和。

public static void main(String[] args) {
    final int SIZE = 10;
    int sumList1 = 0;
    int sumList2 = 0;
    Die[] DieList = new Die[SIZE];
    ArrayList < Die > DieList2 = new ArrayList < Die > ();

    for (int i = 0; i < SIZE; i++) {
        DieList[i] = new Die();
        DieList2.add(new Die());
        DieList[i].roll();
        DieList2.get(i).roll();
        sumList1 += DieList[i].getFaceValue();
        sumList2 += DieList2.get(i).getFaceValue();
    }

    System.out.println("Sum of Array: " + sumList1);
    System.out.println("Sum of ArrayList: " + sumList2);
    System.out.println("Sum of both: " + (sumList1 + sumList2));
}