Powered by Ajaxy
Digg StumbleUpon LinkedIn YouTube Flickr Facebook Twitter RSS Reset

Java: How to resize an array

 int,string,char,static, url, socket, url, socket, java

 * @(#)UpdateArraySize.java
 * @author Sakoba
 * @version 1.00 2012/12/1
import java.util.*;
import java.lang.reflect.Array;
public class UpdateArraySize {
    public static void main (String[] args) 
    		String array[]={"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};
    		// Print original size
    		System.out.printf("original size=>%s\nContent=>%s\n",
    		  array.length, Arrays.toString(array));
    		 // resize the array
    		 array=(String[])resizeArray(array, 2);
    	    System.out.printf("After changing size=>%s\nContent=>%s",
    		  array.length, Arrays.toString(array));
      public static Object resizeArray(Object a, int newLength) 
    	Class cl = a.getClass();
    		if (!cl.isArray()) return null;
    	int length = Array.getLength(a);
        Class componentType = a.getClass().getComponentType();
        Object newArray = Array.newInstance(componentType, newLength);
        System.arraycopy(a, 0, newArray, 0, newLength);
        return newArray;
original size=>5
Content=>[Monday, Tuesday, Wednesday, Thursday, Friday]
After changing size=>2
Content=>[Monday, Tuesday]

Users who found this page were searching for:

  • to find n factorial in perl
  • definition of array resizing java
  • iterate over tuple
  • java array resize
  • sun ftpclient java net sample
  • java JButton examples JDK7
  • calendar in jpanel
  • java %tt
  • tkinter input output
  • python get ip by host name in local network

No comments yet.

Leave a Comment