0

So I'm just learning about arrays in Java (interesting stuff) but I'm having some problems getting my head around the contains() method.

I tried:

Random rn = new Random();

        int first = 12;
        int[] tab = new int[first];

        for (int i = 0; i <= first - 1; i++) {
            tab[i] = rn.nextInt(10);

Which seemed to work fine for filling in my Array, but then I tried a:

System.out.println(Arrays.asList(tab).contains(9));

And no matter what, even if I fill the array manually with 9's, it'll still only print up "false".

What am I doing wrong?

ViRALiC
  • 1,419
  • 4
  • 18
  • 46
  • @ZouZou Do you have it on shortcut? – Sotirios Delimanolis Jan 14 '14 at 18:17
  • 2
    That returns an `ArrayList` with one element, – SLaks Jan 14 '14 at 18:17
  • 2
    @SotiriosDelimanolis I was just reading [this](http://stackoverflow.com/questions/21120554/arrays-aslist-contains-on-double-vs-double-arrays#21120615) question asked 20 mins ago :) – Alexis C. Jan 14 '14 at 18:18
  • @SLaks Any chance you could elaborate, friend? – ViRALiC Jan 14 '14 at 18:21
  • And @Zouzou, it looks similar but not what I was looking for, sadly :( – ViRALiC Jan 14 '14 at 18:22
  • Hey, who closed this? I have just typed a fairly long and detailed answer - but not quickly enough. It's very similar to that other question, but I don't think it's an exact duplicate. Voting to reopen. – Dawood ibn Kareem Jan 14 '14 at 18:25
  • OK, never mind. I made part of it into a comment on that other answer. Grumpy about the rep though. – Dawood ibn Kareem Jan 14 '14 at 18:27
  • @DavidWallace Y-yeah, it does look similar, but it really isn't the same question. Anyway, we got an answer and it was flawless in all its ways so this question is pretty much done. – ViRALiC Jan 14 '14 at 18:27
  • I wouldn't say "flawless". It seems to me that your question was seeking an explanation - "what am I doing wrong?" - rather than a "try this". If I thought the answer provided that, I wouldn't have provided my own explanation. – Dawood ibn Kareem Jan 14 '14 at 18:30
  • @DavidWallace Hehe, yeah I guess. I'm just surprised I got help this quick. First question I ever dared ask. Thanks a bundle though, for explaining it to me! I really do appreciate it! – ViRALiC Jan 14 '14 at 18:31
  • @ViRALiC You're right that's not an exact duplicate it does not provide the reason why your code doesn't work as expected. You can check this question (that I've put in comments on the top) : http://stackoverflow.com/questions/21120554/arrays-aslist-contains-on-double-vs-double-arrays#21120615 – Alexis C. Jan 14 '14 at 19:44

1 Answers1

0

Try this

Integer[] tab = new Integer[]{9};
System.out.println(java.util.Arrays.asList(tab).contains(9));

I get

true
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • I think my mistake was in the use of "int" instead of "Integer". Everything worked out perfectly with "Integer" instead. Any idea why this is? Your answer was perfect though! Thank you very much! – ViRALiC Jan 14 '14 at 18:25
  • @ViRALiC Combined with auto-boxing; yes. You were getting a `List` because generics don't allow for primitive types. – Elliott Frisch Jan 14 '14 at 18:26
  • 1
    The problem is with `Arrays.asList()` - the type of its parameter is `Object...`, which means you can pass either an `Object[]` argument or any number of individual `Object` arguments. Now, `tab` is an `int[]`, which is NOT a type of `Object[]`. It IS however a type of `Object`, so it is interpreted as a single argument. You end up with a list of one element, which is `tab`, rather than a list of 12 elements. If you declare `tab` as an `Integer[]` instead of an `int[]`, your program will work as you intend, because `Integer[]` IS a subtype of `Object[]`. – Dawood ibn Kareem Jan 14 '14 at 18:26
  • Ah, I see, thank you, both of you. Still new to all this and your answers were very helpful! Appreciated! – ViRALiC Jan 14 '14 at 18:28