How can I add new elements to an Array in Java

0 votes

I want to append elements to an array

String[] mm= {"5","4","3"};
mm.append("2");

This gives me a compile-time error

Please give me a way in which I can add elements to this array.

Apr 19, 2018 in Java by Parth
• 4,630 points
11,342 views

3 answers to this question.

0 votes

Java arrays are fixed in size. You cannot append elements in an array.

Instead, we can use an ArrayList object which implements the List interface.

An ArrayList can dynamically increase and decrease its size and thus it can append new elements to it for example :

List ll=new ArrayList("5","4","3");

ll.add("2");
answered Apr 19, 2018 by developer_1
• 3,320 points
ArrayList<String> myList = new ArrayList<String>(Arrays.adlist(myArray));

System.out.println("Enter elements to be added");

String element=sc.next();

myList.add(element);

myArray=myList.toArray(myList);

System.out.println(Arrays.toString(myArray));
0 votes

We can also add elements to the array without using complex objects and collections.

String[] array1 = new String[]{"one", "two"};
String[] array2 = new String[]{"three"};
String[] array = new String[array1.length + array2.length];
System.arraycopy(array1, 0, array, 0, array1.length);
System.arraycopy(array2, 0, array, array1.length, array2.length);
answered Jul 19, 2018 by Sushmita
• 6,910 points
0 votes
String[] source = new String[] { "a", "b", "c", "d" };
String[] destination = new String[source.length + 2];
destination[0] = "/bin/sh";
destination[1] = "-c";
System.arraycopy(source, 0, destination, 2, source.length);

for (String parts : destination) {
  System.out.println(parts);
}
answered Sep 19, 2018 by Sushmita
• 6,910 points

Related Questions In Java

0 votes
2 answers
0 votes
2 answers

How an object array can be converted to string array in java?

System.arraycopy is the most efficient way, but ...READ MORE

answered Aug 8, 2018 in Java by Sushmita
• 6,910 points
4,691 views
0 votes
2 answers

How can I convert a String variable to a primitive int in Java

 Here are two ways illustrating this: Integer x ...READ MORE

answered Aug 20, 2019 in Java by Sirajul
• 59,230 points
1,874 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
851 views
+1 vote
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,620 points
943 views
+1 vote
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,290 points
958 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,220 points
3,127 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,220 points
684 views
0 votes
2 answers

How do I convert an input stream into a byte array

You could probably try this if you ...READ MORE

answered Aug 29, 2019 in Java by Sirajul
• 59,230 points
3,344 views
0 votes
2 answers

How can I invoke a method when the method name is in the form of a given string?

You could probably use method invocation from reflection: Class<?> ...READ MORE

answered Aug 19, 2019 in Java by Sirajul
• 59,230 points
2,486 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP