Appending to arrays/array help

J

JamesG

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!
 
J

JamesG

Sorry should mention, the array is of predetermined size, i.e. :

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

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

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

}

return c;
}

c[] being the array in question. Thanks
 
P

Patricia Shanahan

JamesG said:
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
 
J

JamesG

JamesG said:
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


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?
 
P

Patricia Shanahan

JamesG said:
JamesG said:
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


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?

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
 
J

JamesG

JamesG said:
JamesG wrote:
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
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?

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


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

Thanks
 
P

Patricia Shanahan

JamesG said:
JamesG said:
JamesG wrote:
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
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?
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


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

Thanks


Do you know the max length for a given number in advance? If so,
consider byte[][] array, and you would access array[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
 
L

Lew

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


Patricia said:
Do you know the max length for a given number in advance? If so,
consider byte[][] array, and you would access array[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
 
P

Patricia Shanahan

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


Patricia said:
Do you know the max length for a given number in advance? If so,
consider byte[][] array, and you would access array[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.


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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top