Extend an array after initialization

0 votes
Please help me with the way of extending an array after initializing it.
Mar 11 in Java by Suresj
34 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 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,190 points
108 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,190 points
33 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,840 points
50 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 parth295
• 4,600 points
49 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,540 points
42 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,280 points
32 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,190 points
196 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,190 points
39 views
0 votes
1 answer

Reverse an Array in Java

To reverse an array, here is the ...READ MORE

answered Mar 11 in Java by Esha Gupta
34 views
0 votes
1 answer

How to remove object from an Array?

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

answered Mar 11 in Java by Esha Gupta
34 views