Java – Append values into an Object[] array

In this example, we will show you how to append values in Object[] and int[] array.


    Object[] obj = new Object[] { "a", "b", "c" };
    ArrayList<Object> newObj = new ArrayList<Object>(Arrays.asList(obj));
    newObj.add("new value");
    newObj.add("new value 2");	

1. Object[] Array Example

Example to append values with ArrayList :

TestApp.java

package com.mkyong.test;

import java.util.ArrayList;
import java.util.Arrays;

public class TestApp {

  public static void main(String[] args) {
	TestApp test = new TestApp();
	test.process();
  }

  private void process() {

	Object[] obj = new Object[] { "a", "b", "c" };

	System.out.println("Before Object [] ");
	for (Object temp : obj) {
		System.out.println(temp);
	}

	System.out.println("\nAfter Object [] ");
	Object[] newObj = appendValue(obj, "new Value");
	for (Object temp : newObj) {
		System.out.println(temp);
	}

  }

  private Object[] appendValue(Object[] obj, Object newObj) {

	ArrayList<Object> temp = new ArrayList<Object>(Arrays.asList(obj));
	temp.add(newObj);
	return temp.toArray();

  }

}

Output


Before Object [] 
a
b
c

After Object [] 
a
b
c
new value

2. int[] Array Example

To append values in primitive type array – int[], you need to know how to convert between int[] and Integer[]. In this example, we use the ArrayUtils class, from Apache common third party library to handle the conversion.

TestApp2.java

package com.hostingcompass.test;

import java.util.ArrayList;
import java.util.Arrays;
import org.apache.commons.lang3.ArrayUtils;

public class TestApp2 {

  public static void main(String[] args) {
	TestApp2 test = new TestApp2();
	test.process();
  }

  private void process() {

	int[] obj = new int[] { 1, 2, 3 };
	System.out.println("Before int [] ");
	for (int temp : obj) {
		System.out.println(temp);
	}

	System.out.println("\nAfter Object [] ");
		
	int[] newObj = appendValue(obj, 99);
	for (int temp : newObj) {
		System.out.println(temp);
	}

  }

  private int[] appendValue(int[] obj, int newValue) {

	//convert int[] to Integer[]
	ArrayList<Integer> newObj = 
		new ArrayList<Integer>(Arrays.asList(ArrayUtils.toObject(obj)));
	newObj.add(newValue);
		
	//convert Integer[] to int[]
	return ArrayUtils.toPrimitive(newObj.toArray(new Integer[]{}));

  }

}

Output


Before int [] 
1
2
3

After Object [] 
1
2
3
99

The int to Integer conversion is a bit weird… Do let me know if you have a better idea.

References

  1. Apache ArrayUtils JavaDoc
  2. Java – Convert int[] to Integer[] example
author image

mkyong

Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities. Read all published posts by

Comments

avatar
3000
newest oldest most voted
Jubilee Happy
Guest
Jubilee Happy

This is a good example. However, I’d like to know how to read text string (from a file) to a 2D array, i.e, Object[][]. Input string – {“a”, “b”, “c”}, output string – {{“a”, “b”, “c”}} {“d”, “e”, “f”}}

Gokul
Guest
Gokul

How to do it for Java – Append values into an Object[][] array? I tried with conversion as you did, but no luck

vin
Guest
vin

can you tell about String[] array? it’s important for me to use only String[] because of the output capability of other end.

Mirela
Guest
Mirela

Thanks!!!!!!

guest
Guest
guest

The int to Integer conversion is a bit weird indeed, I’d go for:

private int[] append(int[] orig, int … append) {
int[] result = Arrays.copyOf(orig, orig.length + append.length);
System.arraycopy(append, 0, result, orig.length, append.length);
return result;
}

No benchmarking done, but I’d expect direct array copy to perform a bit better, without the need to do the extra conversions.

Oh, and I took the liberty to change the second argument to varargs, in case someone would want to append several additional values at a time.

Umashankar
Guest
Umashankar

System.arraycopy widely used in internals of Java datastructures (collections). More over some calculations are done in bitwise arithmetic for performance.