Extend an array after initialization

0 votes
Please help me with the way of extending an array after initializing it.
Mar 11, 2019 in Java by Suresj
227 views

1 answer to this question.

0 votes

Here is the method I came up with, please let me as it works fine or not,

public class Main {
   public void extendArraySize() {
      String[] names = new String[] {"c", "b", "a"};
      String[] extended = new String[5];
      extended[3] = "p";
      extended[4] = "q";
      System.arraycopy(names, 0, extended, 0, names.length);
      
      for (String str : extended) System.out.println(str);
   } 
   public static void main(String[] args) {
      new Main().extendArraySize();
   }
}
answered Mar 11, 2019 by Esha Gupta

Related Questions In Java

0 votes
2 answers

One line initialization of an ArrayList object in Java

In Java 8 or earlier: List<String> string = ...READ MORE

answered Jul 26, 2018 in Java by samarth295
• 2,200 points
1,271 views
0 votes
2 answers

How to convert an array to arraylist?

In Java 9 you can use: List<String> list= List.of("Hello", "World", ...READ MORE

answered Aug 1, 2018 in Java by samarth295
• 2,200 points
140 views
0 votes
2 answers

How to test that an array contains a certain value?

public static final String[] VALUES = new ...READ MORE

answered Jul 17, 2018 in Java by Sushmita
• 6,890 points
137 views
0 votes
3 answers

How to sort an array in java?

import java.util.Arrays; public class Sort { ...READ MORE

answered Aug 24, 2018 in Java by Parth
• 4,630 points
257 views
0 votes
1 answer

Are arrays equivalent to objects in Java ?

Yes; the Java Language Specification writes: In the Java ...READ MORE

answered May 10, 2018 in Java by Rishabh
• 3,580 points
237 views
0 votes
1 answer

Remove objects from an array in Java?

We can use external libraries: org.apache.commons.lang.ArrayUtils.remove(java.lang.Object[] array, int ...READ MORE

answered Jun 26, 2018 in Java by scarlett
• 1,270 points
225 views
+1 vote
3 answers

What is the syntax to declare and initialize an array in java?

You can use this method: String[] strs = ...READ MORE

answered Jul 25, 2018 in Java by samarth295
• 2,200 points
1,893 views
0 votes
2 answers

What is the syntax to initialize an array?

Rather than learning un-Official websites learn from ...READ MORE

answered Aug 2, 2018 in Java by samarth295
• 2,200 points
155 views
0 votes
2 answers

Reverse an Array in Java

If you want to reverse the array ...READ MORE

answered Aug 28, 2019 in Java by Sirajul
• 58,210 points
85 views
0 votes
1 answer

How to remove object from an Array?

Here is a code I came up ...READ MORE

answered Mar 11, 2019 in Java by Esha Gupta
93 views