Checking if an array contains certain integers
Asked Answered
R

3

0

I am making an Array that is 10 integers long, so that each place 0-9 contains a different integer 0-9.

I am having trouble figuring out how to check if the array already contains a certain number, and if so, regenerating a new one.

So far I have:

for (int i = 0; i < perm.length; i++)
{
    int num = (int) (Math.random() * 9); 
    int []

    perm[i] = num;   
}
Railey answered 9/12, 2012 at 16:7 Comment(2)
The accepted answer is not correct. Have u checked it???Remy
Possible duplicate of Java, Simplified check if int array contains intFactorial
D
4
Arrays.asList(perm).contains(num) 

from How can I test if an array contains a certain value?

for (int i = 0; i < perm.length; i++)

this is not enough to loop like this, if collision happens some slots would have been not initalized.

Overall, for this task you better initialize array with values in order and then shuffle it by using random permutation indexes

Diggins answered 9/12, 2012 at 16:12 Comment(10)
I am new to java and just started learning about arrays. Could you give me an example of what you mean or explain it?Railey
@user1729448, explain about what? Have you followed the provided links?Diggins
Then Arrays.asList(perm).contains(num) answers you question exactly.Diggins
Never mind I understand it. So if the num is on the list it returns true?Railey
@user1729448, yes, it should.Diggins
let us continue this discussion in chatRailey
This is wrong.. Arrays.asList(perm) returns a List<int[]>. contains may not always be true, even if the number is in there.Pierce
This is wrong. in contains(num) num should be an object. But it will never be the same object even if that number was found early. Because here for each int value a new Integer object is created and passed into the method.Remy
This will only work for Integer object. Not with primitive int.Peroxidase
This is wrong...per the above comments from @WaiYanBarbule
R
0

here is a complete answer

int[] array = {3,9, 6, 5, 5, 5, 9, 10, 6, 9,9};

    SortedSet<Integer> s = new TreeSet();
    int numberToCheck=61;
    for (int i = 0; i < array.length; i++) {
        s.add(array[i]);
    }

    System.out.println("Number contains:"+!(s.add(numberToCheck)));


    System.out.println("Sorted list:");
    System.out.print(s);
Remy answered 20/5, 2015 at 19:15 Comment(2)
Why do you need to define "mode" and "maxCount" variabled?Atman
Sorry for the confusion. They are not needed. I edited the answer removing themRemy
B
0

Adding both string and integer to check is exist or not.

public class existOrNotInArray {

public static void elementExistOrNot() {
    // To Check Character exist or not
    String array[] = { "AB", "CD", "EF" };

    ArrayList arrayList = new ArrayList<>();

    String stringToCheck = "AB";

    for (int i = 0; i < array.length; i++) {
        arrayList.add(array[i]);
    }

    System.out.println("Character exist : " + arrayList.contains(stringToCheck));

    // To Check number exit or not
    int arrayNum[] = { 1, 2, 3, 4 }; // integer array

    int numberToCheck = 5;

    for (int i = 0; i < arrayNum.length; i++) {
        arrayList.add(arrayNum[i]);
    }
    System.out.println("Number exist :" + arrayList.contains(numberToCheck));
}

public static void main(String[] args) {
    elementExistOrNot();
}
}
Bernetta answered 3/8, 2018 at 16:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.