2

I'm writing a simple Java method. I have instantiated the array to be iterated over

int[] j = new int[8];

The array is then populated with random values between 1 and 100.

for (int i = 0; i < j.length; i++){
        j[i] = (int)(Math.random() * 100);
}

Now I'm trying to iterate over this loop, while replacing a line when the iteration number is found in the array, using Arrays.asList. I can't figure out why the replacement isn't happening in the following statement.

for(int i = 1; i <= 100; i++){

    //Iterate over the array
    if(Arrays.asList(j).contains(i)){
        System.out.println(i + " Random Typo");
    }
    else{
        System.out.println(i + " I will never spam my friends again.");
    }
}

I know that I could loop over the array and compare, but I'd like to get the asList.contains method to work so that the array doesn't have to iterated over 100 times. The output prints "i I will never spam my friends again." on each pass instead of making the replacements. Does anyone see an error in my syntax? I don't have any import errors.

natep
  • 126
  • 4
  • 13

1 Answers1

4

You can't have a list of primitive type.

change j to

Integer [] j=new Integer[8];
bob-cac
  • 1,272
  • 2
  • 17
  • 35