仅打印数组中3的倍数

时间:2015-12-13 20:55:22

标签: java arrays algorithm indexoutofboundsexception

我正在尝试完成一项练习,其中包含以下任务: 仅打印数组中3的倍数 我必须使用applet,我不知道该怎么做。 我试图在图形部分设置条件,但它返回一个不太好的0

public void init() {
  dataList = new int[17];
  int dataList[] = {2,4,6,9,5,4,5,7,12,15,21,32,45,5,6,7,12};

  for (int i = 0; i < dataList.length; i++) {
    //Compute the sum of the elements in the array.
    sum += dataList[i];
    //Compute the product of the elements in the array.
    product *= dataList[i];
    //Compute the frequency of the number 5 in the array
    if (dataList[i] == 5) {
      fiveCounter++;
    }
  }
}

public void paint(Graphics g) {
  g.drawString(("Sum of elements is: " + sum), 25, 25);
  g.drawString(("Product of elements is: " + product), 25, 50);
  g.drawString(("Number 5 is present " + fiveCounter + " times"), 25, 75);
  for (int i = 0; i < dataList.length; i++) {
    if ((dataList[i] % 3) == 0) {
      g.drawString((String.valueOf(dataList[i])), 25, 100);
    }
  }
}

在另一次尝试中,我尝试根据3的多个值的计算值创建一个新数组,程序无法启动,我得到ArrayIndexOutOfBoundException

public void init() {
  dataList = new int[17];
  multiple3 = new int[mult3Counter];
  int dataList[] = {2,4,6,9,5,4,5,7,12,15,21,32,45,5,6,7,12};

  for (int i = 0; i < dataList.length; i++) {
    //Compute the sum of the elements in the array.
    sum += dataList[i];
    //Compute the product of the elements in the array.
    product *= dataList[i];
    //Compute the frequency of the number 5 in the array
    if (dataList[i] == 5) {
      fiveCounter++;
    }
    if ((dataList[i] % 3) == 0) {
      multiple3[i] = dataList[i];
      mult3Counter++;
    }
  }

public void paint(Graphics g) {
  g.drawString(("Sum of elements is: " + sum), 25, 25);
  g.drawString(("Product of elements is: " + product), 25, 50);
  g.drawString(("Number 5 is present " + fiveCounter + " times"), 25, 75);
  for (int i = 0; i < multiple3.length; i++) {
    g.drawString((String.valueOf(multiple3[i])), 25, 100);
  }
}

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

您不能对两个阵列使用相同的计数器。使用两个不同的柜台。您无法使用mult3Counter作为数组multiple3的大小,因为未初始化!因此,mult3Counter默认为0。因此,当您要使用任何索引访问multiple3[]时,它会提供ArayIndexOutOfBoundsException

如果你需要知道3的倍数的出现次数,你必须运行两次循环;

int mult3Counter = 0;
for (int i = 0; i < dataList.length; i++) {
  if ((dataList[i] % 3) == 0) {
    mult3Counter++;
  }
}

int j = 0;
int [] multiple3 = new int[mult3Counter];

for (i = 0; i < dataList.length; i++)
{
  if ((dataList[i] % 3) == 0)
  {
    multiple3[j++] = dataList[i];
  }
}

或者最好的方法是使用ListArrayList)来添加3的倍数。

ArrayList<int> multiple3 = new ArrayList<>();

for (int i = 0; i < dataList.length; i++) {
  if ((dataList[i] % 3) == 0) {
    multiple3.add(dataList[i]);
  }
}

如果需要数组,可以稍后将其转换为数组。请参阅This Question

答案 1 :(得分:0)

我自己解决了问题,只更改了图形部分中的代码。 唯一丑陋的是我不得不重复dataList,因为我无法从init方法传递值:

public void paint(Graphics g) {
  int dataList[] = {2,4,6,9,5,4,5,7,12,15,21,32,45,5,6,7,12};
  int yposition = 100;

  g.drawString(("Sum of elements is: " + sum), 25, 25);
  g.drawString(("Product of elements is: " + product), 25, 50);
  g.drawString(("Number 5 is present " + fiveCounter + " times"), 25, 75);
  g.drawString(("The following numbers are multiple of 3"), 25, yposition);

  for (int i = 0; i < dataList.length; i++) {
    if ((dataList[i] % 3) == 0) {
      yposition += 15;
      g.drawString((String.valueOf(dataList[i])), 25, yposition);
    }
  }
}