increasing size of String array

  • Thread starter Miss. Michelle Heigardt
  • Start date
M

Miss. Michelle Heigardt

Hi, I have a string array

String[] counting={"eine","zwei","drei","vier"};

and I want to increase size to include "five". But I do not want to do it
by modifying the declaration like this:

String[] counting={"eine","zwei","drei","vier","fünf"};

I want to increase the size runtime.

Is there a simpler way of doing this other than

String[] counting={"eine","zwei","drei","vier"};
String[] c=new String[counting.length+1];
for (int i=0;i<counting.length;i++)
c=counting;
c[counting.length]="fünf";
counting=c;


Thank you
Michelle
 
C

Christophe Vanfleteren

Miss. Michelle Heigardt said:
Hi, I have a string array

String[] counting={"eine","zwei","drei","vier"};

and I want to increase size to include "five". But I do not want to do it
by modifying the declaration like this:

String[] counting={"eine","zwei","drei","vier","fünf"};

I want to increase the size runtime.

Is there a simpler way of doing this other than

String[] counting={"eine","zwei","drei","vier"};
String[] c=new String[counting.length+1];
for (int i=0;i<counting.length;i++)
c=counting;
c[counting.length]="fünf";
counting=c;


Thank you
Michelle


You could use a List, they resize automatically if needed.
Or you could use the System.arrayCopy method.
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top