将数据放入 Java 数组

Putting data into Java arrays

我有一个关于数组如何存储数据以及如何正确地将数据放入数组的基本问题。

THIS PART ANSWERED

In this code the method spinWheel() is just calling an integer from 0-36.

    for(cntr=0; cntr<99; cntr++)
        {
            spunNum=spinWheel();
            all99Spun[0]=spunNum;
        }

How do I adjust the array all99Spun[] so that the next time the loop executes it would put spunNum into all99Spun[1] and so forth?

关于数组的另一个问题是如何检查 1 个整数与存储在数组中的所有整数之间的相等性。

例如,我有一个存储轮盘所有红色数字的数组。 int redNumbers[] = new int[] {1,3,5,7,9,12,14,16,18,19,21,23,25,27,30,32,34,36}; 我如何检查存储在 all99Spun 数组中的数字是否与 rednumbers 数组中的整数相等?

改变一下

all99Spun[0]=spunNum;

all99Spun[cntr]=spunNum;

根据我对你第一个问题的理解,我认为@Gregory 的回答是正确的。

所以我会尝试回答你的第二个问题。

Another question I have about arrays is how to check equality between 1 integer and all the integers stored in an array.

有两种简单的方法可以判断整数数组是否相等。

  1. 循环遍历,保留对前一个数字的引用。当你发现一个不等于前一个的那一刻,你就知道它们并不完全相等。
  2. 因为你用的是Java,你可以把它们都加到一个Set里,保证唯一性。如果它们都相等,则将它们全部添加到集合中后,集合应该只包含 1 个元素。

第一个示例:

for (int i = 0; previous = redNumbers[i]; i < redNumbers.length; i++) {
    if (redNumbers[i] != previous) {
        // they are not all equal.
    }
    previous = redNumbers[i];
}

第二个示例:

Set<Integer> distinct = new TreeSet<>(Arrays.asList(redNumbers));
if (distinct.size() > 1) {
    // they are not all equal.
}

改变一下

all99Spun[0]=纺纱数量; 至

all99Spun[cntr]=纺纱数量;

回答第二个问题,我想你只是想看看旋转数组中的任何一个数字是否存在于读取数组中。如果您想检查读取数组中是否只存在一个数字,您可以循环直到找到该数字:

int num = all99Spun[0];
int index = -1;
for(int i = 0; i < redNumbers.length ; i++)
{
     if(redNumbers[i] == num)
     {
           index = i;
           break;
     }
}

如果末尾索引不等于 -1,则该数字在 redNumbers 数组中。

如果要检查整个数组:

for(int i = 0 ; i < all99Spun.length ; i++)
{
    for(int j = 0; j < redNumbers.length; j++)
    {
          if(redNumbers[j] == all99Spun[i])
          {
                //doWork
          }
    }
}