-2

I have a fundamental question about how arrays store data and how to properly put data into an array.

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?

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

for example I have an array that stores all the red numbers of a roulette wheel. int redNumbers[] = new int[] {1,3,5,7,9,12,14,16,18,19,21,23,25,27,30,32,34,36}; How would I check the equality of a number stored in the all99Spun array with that of the integers in the array of rednumbers?

3
  • Are you asking how do you index an array with a variable? And then are you asking how to access integers in an array to compare them? These are both fundamentals in pretty much any language Commented Feb 20, 2015 at 23:06
  • Check this: How to compare Integer with integer array stackoverflow.com/questions/5269183/… Commented Feb 20, 2015 at 23:09
  • try to use ArrayList<Integer> red = new ArrayList<Integer>(); to store your red numbers and then call if(red.contains(some_integer)) Commented Feb 20, 2015 at 23:19

3 Answers 3

1

Just change

all99Spun[0]=spunNum; to

all99Spun[cntr]=spunNum;

To answer the second question, I think you just want to see if any one number from the spun array exists in the read array. If you want to check that just one number exists in the read array you can loop through until you find that number:

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

if at the end index does not equal -1, then the number was in the redNumbers array.

If you want to check it for the entire array:

for(int i = 0 ; i < all99Spun.length ; i++)
{
    for(int j = 0; j < redNumbers.length; j++)
    {
          if(redNumbers[j] == all99Spun[i])
          {
                //doWork
          }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

Just change

all99Spun[0]=spunNum;

to

all99Spun[cntr]=spunNum;

Comments

0

Given the way I understand your first question, I think @Gregory's answer is right.

So I will try to answer your second question.

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

There are 2 easy ways to see if an array of integers are equal.

  1. Iterate through with a loop, keeping a reference to the previous number. The minute you find one that does not equal the previous one, you know they are not all equal.
  2. Since you are using Java, you can add them all to a Set, which guarantees uniqueness. If they are all equal, then after adding them all to the set, the set should contain only 1 element.

Example of first:

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

Example of second:

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

2 Comments

I think the question was to check if a number (result a spin wheel) is in a list of numbers (here the reds). Anyway, using a Set is better than looping through all elements in the array.
I see. The question was confusing, so I tried to answer it as best I could. If they just want to see if a number is in a list of numbers like you said, then a TreeSet is a good solution. Since the question was about arrays, if the OP needs to stick to arrays, then a binary search would work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.