Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2007

Tip: Looking for answers? Try searching our database.

Appending to arrays/array help

Thread view: 
JamesG - 07 Mar 2007 19:19 GMT
Hi, I have a data structure as follows:

fig. 1: int[] array = {15829289595299, 23848108482942,
2842948284422, ...};

The numbers in the array might be 750 digits long.

Firstly, is this the best method of storing the numbers? or should I
do an array of arrays so each number has an array of chars?

Next, assuming the array in fig. 1, how would I append extra digits to
each array item? I dont want to += add a new digit to the total value,
but add it on the end.

Thanks in advance!
JamesG - 07 Mar 2007 19:49 GMT
Sorry should mention, the array is of predetermined size, i.e. :

   public int[] foo(int s) {
       c = new int[s];

       for(int i=0; i<s; i++) {

           for(int n: somearray) {
               c[i] = random.nextInt();
           }

       }

       return c;
   }

c[] being the array in question. Thanks
Patricia Shanahan - 07 Mar 2007 20:03 GMT
> Hi, I have a data structure as follows:
>
> fig. 1: int[] array = {15829289595299, 23848108482942,
> 2842948284422, ...};

This will not work, because the numbers are bigger than Integer.MAX_VALUE.

Have you looked at java.math.BigInteger?

Patricia
JamesG - 07 Mar 2007 20:14 GMT
> > Hi, I have a data structure as follows:
>
[quoted text clipped - 6 lines]
>
> Patricia

I thought this might be the case.
I think I might be better storing the numbers in a string. I can then
access each digit and use .append to add digits right?
Patricia Shanahan - 07 Mar 2007 20:33 GMT
>>> Hi, I have a data structure as follows:
>>> fig. 1: int[] array = {15829289595299, 23848108482942,
[quoted text clipped - 8 lines]
> I think I might be better storing the numbers in a string. I can then
> access each digit and use .append to add digits right?

If you go with a string-like solution and need to append often, consider
StringBuilder. Do you need to do arithmetic on these things? That might
make the difference between BigInteger and StringBuilder.

Patricia
JamesG - 07 Mar 2007 20:42 GMT
> >>> Hi, I have a data structure as follows:
> >>> fig. 1: int[] array = {15829289595299, 23848108482942,
[quoted text clipped - 14 lines]
>
> Patricia

No arithmetic required as such. I just need to extract digits at
certain positions in the array.
Could i do this like: array[i][300] to get the 299th digit of i'th
array?

Thanks
Patricia Shanahan - 07 Mar 2007 20:47 GMT
>>>>> Hi, I have a data structure as follows:
>>>>> fig. 1: int[] array = {15829289595299, 23848108482942,
[quoted text clipped - 17 lines]
>
> Thanks

Do you know the max length for a given number in advance? If so,
consider byte[][] array, and you would access array[i][300].

If you don't know a reasonably accurate maximum length, consider using
an array of StringBuilder. It will handle extending the array for you.

Patricia
Lew - 07 Mar 2007 21:41 GMT
JamesG wrote:
>> No arithmetic required as such. I just need to extract digits at
>> certain positions in the array.
>> Could i do this like: array[i][300] to get the 299th digit of i'th
>> array?

> Do you know the max length for a given number in advance? If so,
> consider byte[][] array, and you would access array[i][300].
>
> If you don't know a reasonably accurate maximum length, consider using
> an array of StringBuilder. It will handle extending the array for you.

Or even List<StringBuilder>.

Then you can have an arbitrarily large list of arbitrarily large CharSequences
of varying lengths.

To the OP: Have you looked at the API docs for StringBuilder since Patricia
suggested the class?

BigInteger?

After those, look at List and its implementations.

If you don't research the suggestions how will you know if they'll help?

-- Lew
Patricia Shanahan - 07 Mar 2007 21:45 GMT
> JamesG wrote:
>>> No arithmetic required as such. I just need to extract digits at
[quoted text clipped - 19 lines]
>
> After those, look at List and its implementations.

I'd even consider an ArrayList of ArrayList of Byte, with just 10
distinct Byte objects, one for each value. It would cost a 32 or 64 bit
reference for each element, less space efficient than StringBuilder.

> If you don't research the suggestions how will you know if they'll help?

Especially important because I don't think we have been given enough
size and operation frequency data to actually pick a really good
structure. All we can do is suggest structures to consider.

Patricia


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.