0

Here's the relevant lines of code:

HashSet<Integer> products = new HashSet<Integer>();  
...  
Integer[] arrProducts = prodDigs.toArray();`

Any idea why I'm getting the error stated in the question?

I'm using BlueJ as my IDE if it's at all relevant.

Vishal Chhodwani
  • 2,567
  • 5
  • 27
  • 40
  • 4
    What is the type of prodDigs? – David Brossard Jun 06 '18 at 05:17
  • this code is not enough to identify the issue. proDigs may not be created on it may not visible to your code block – Sasikumar Murugesan Jun 06 '18 at 05:20
  • 2
    Is it a typo? should it be `products.toArray()` ? – yellowB Jun 06 '18 at 05:20
  • Welcome to Stackoverflow, please read [How To Ask](https://stackoverflow.com/help/how-to-ask). Pay special attention to [How To Create MCVE](https://stackoverflow.com/help/mcve). Make sure you tag your question with proper labels (programming language, relevant technologies etc). The more effort you'll put into posting a good question: one which is easy to read, understand and which is [on topic](https://stackoverflow.com/help/on-topic) - the chances are higher that it will attract the relevant people and you'll get help even faster. Good luck! – Nir Alfasi Jun 06 '18 at 05:23
  • Yes, it was a typo. It should indeed have been `products.toArray()`. As for prodDigs, it's an int[] and should not be referred to in this line. Lesson learned: don't write code after midnight. – Thomas Riley Jun 06 '18 at 17:14

2 Answers2

1

With your little decription, I could guess you want to convert hash set to array using to Array():

To convert your hash set to integer array you can use :

Integer[] arrProducts = products.toArray(new Integer[products.size()]);
Adya
  • 1,084
  • 9
  • 17
  • It would be nice to explain, first the typo, then why you use `AbstractCollection.toArray(T[])` instead of `AbstractCollection.toArray()`. – AxelH Jun 06 '18 at 05:35
  • Ah, found the typo - that's not in my code, that's a result of being a first-time poster on Stack Overflow, and as such, messing up the formatting @AxelH – Thomas Riley Jun 06 '18 at 13:49
  • Although yes, I would like an explanation as to why use toArray(T[]) instead of toArray(). I think I've figured it out after reading the documentation now that I know that you should do that in my case (it's because I've instantiated my HashSet, isn't it?) – Thomas Riley Jun 06 '18 at 13:53
  • @AxelH found the typo, and I can't make an edit to my comment yet, but must leave for class before I'll be able to. That's why you shouldn't code at 1 AM. – Thomas Riley Jun 06 '18 at 14:00
  • Don't worry @ThomasRiley , those remarks were addressed to Adya to improve the answer. Not a remark on your code ;) – AxelH Jun 06 '18 at 14:01
0

Just try Java 8 Way

Option 1

Integer arrProducts[]=products.stream().toArray(Integer[]::new);

Option 2

Integer arrProducts[]=products.stream().toArray(n->new Integer[n]);
Juhan
  • 1,283
  • 2
  • 11
  • 30
  • 1
    Would you mind offering an explanation of why this works? I read the stream() method on the documentation for java.util.Collection, but am having trouble figuring out exactly what it does. – Thomas Riley Jun 06 '18 at 13:54