• 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

Regarding array

 
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I have one doubt that is we have declared string array size 10 now we have to insert 15 different string in that array with out change the size of the array can you tell me how to put value into array string.

for example

String a[]=new String[10];


A
B
C
D
E
F
G
H
I
J
K
l
M
N
O

into that array.
Thanking You




 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you need to put 15 strings into an array that can only store 10 strings without being allowed to replace it, you have two options:
1) drop 5 of the strings
2) merge the strings somehow

Either way, you need to find an algorithm to decide which strings to drop, or how to merge strings.
 
Ranch Hand
Posts: 493
Android Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
DO you must use Array?
 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can create a new array with desired size and copy strings from the old array to the new one.
There is an utility function Arrays.copyOf(array, newSize) that does this job:
http://java.sun.com/javase/6/docs/api/java/util/Arrays.html#copyOf%28T[],%20int%29

But if your array will grow, then a better solution is to use ArrayList instead of manually resizing the array.
 
S Shehab
Ranch Hand
Posts: 493
Android Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ireneusz Kordal wrote:You can create a new array with desired size and copy strings from the old array to the new one.
There is an utility function Arrays.copyOf(array, newSize) that does this job:
http://java.sun.com/javase/6/docs/api/java/util/Arrays.html#copyOf%28T[],%20int%29

But if your array will grow, then a better solution is to use ArrayList instead of manually resizing the array.



As Ireneusz said , use ArrayList better in the resizing issue.. beacause it's size will be dynamic ...
 
Ranch Hand
Posts: 492
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't believe creating a new array or using an arraylist solves this particular problem, The issue is inserting 15 strings in a 10 element array without changing the size. Rob's suggestion seems to be the easiest to me.

-Hunter
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic