• 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

A simple programming question to solve.

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello every body.

i am sending some integer type value to a method for factorization.

within the method i want to store the factors in the array , but i do not want extra array spaces in the array and the array size should be the same as no of factors found.



i know how to factorize but the array size is a problem and i do not want to use dynamic arrays in java i just want to use simple plain arrays

i hope you will understand the question.
regards


like e.g the code segment is given below.


 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since arrays must have a fixed size, you really have no choice but to use a collection with a size that changes, like an ArrayList.
ArrayLists are pretty easy to learn how to use, so it wouldn't take you too much effort to learn these.

Or, you could find the factors twice, counting them the first time, and then putting them in the array the second time.
Such as:

int i = 0;
//run the factorization method, adding 1 to i whenever a factor is found. This counts the number of factors.
THEN: int[] arr = new int[i]; //now you have an array that's the right size
//run the factoriation method again, adding each factor to your array.

That's just... a bit inefficient because you have to find the factors twice, but oh well. You don't have much of a choice if you still want to use regular arrays.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I put code tags around your source - you can see how much easier it is to read, as it preserves spacing and does color high-lighting.

and the previous poster is correct. You have given us conflicting requirements. If you don't want to use some dynamically sized array, you must use a fixed size. But, you don't want to use a fixed size due to the wasted space.

You have to decide, as the designer, which requirement is more important, and discard the other.

Or...you could do a total hack...as you find the factors, you could append them to a string with a separator, then parse that when you are done to figure out how many there are...but that is kind of like using a dynamic array since the string will have to change its size to accommodate each additional factor.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Never write == true or == false. write if (b) ... or if (!b) ...

Using the == operator is poor style and error-prone; you might write = instead by mistake.
reply
    Bookmark Topic Watch Topic
  • New Topic