• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Arraylist of variable size in loop?

 
Greenhorn
Posts: 3
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everyone!
I'm trying to write a program to generate prime numbers where numbers are checked against previously found primes. It sounds easy, and I'm sure it is, but I still cannot crack it. This is the code I wrote (and tested) so far:



it compiles fine, but when it runs I get an OutOfMemoryError. If I put a "System.out.println(k)" (and delete the other System.out) inside the inner if (to check what's going on there) I can see values over 10000, values iterate over increasing cycles (e.g. first cycle goes from 1 to 10000, second from 1 to 20000...).
I'm guessing the problem is in the "k<primi.size();". If I add some values to the ArrayList (like I did in the code above) and delete "primi.add(i)" the inner loop seems to work fine.
So I'm guessing the real question is: can I increase the size of an ArrayList in a loop that checks against its size? if not, could you provide different solutions to the problem?

Thank you!>>
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. Change your outer loop (the "i" loop) to only go to 5. It's best to use smaller inputs when debugging, so you don't get overwhelmed with a flood of output.

2. Change your if statement to this:


Printing out relevant values helps you see what's going on.

Now, do you see a serious flaw in your logic? Go back to pencil and paper and try to describe the logic clearly and precisely. When you think you've got it, fix your code. Then step through your code, line by line, and predict what you think will happen at each step. Compare that to what actually happens when you run it. Where the two diverge, there's a problem.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Giovanni Ornaghi wrote:
So I'm guessing the real question is: can I increase the size of an ArrayList in a loop that checks against its size? if not, could you provide different solutions to the problem?



Also, this doesn't make any sense. An ArrayList will increase its size as needed to hold whatever put into it. The problem is that the ArrayList is growing ridiculously huge. (10,000 is not big at all--make the code changes I suggested above to see why it's getting much, much bigger than that.)
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your logic is incorrect. I've added a line printing out i when it's added to the List, and it adds all numbers multiple times. I stopped it at 15 which isn't even a prime number.
Your mistake is that you think that a number is a prime if it's not a multiple of one single previously found prime. Instead, a number is a prime if it's not a multiple of all previously found primes. And you should also not start at 1 but 2 instead, or you will end up with only one "prime number": 1.
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

You have got good answers from good people, so I shall say no more.
 
Jonathan Campbell
Greenhorn
Posts: 3
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all for your help! There was in fact a very stupid flaw in my logic. I've finally come up with a better code. I'm not sure it's the most elegant solution, but it's a working one :)

>
 
reply
    Bookmark Topic Watch Topic
  • New Topic