Thursday, June 8, 2017

Copy Array Data

Tags

Following code will copy the Array Data after changing size with in code:


Code:


public class ArrayDemo {

public static void main(String[] args) 
{
int[] nt=new int[]{7,6,2,3};
  for(int i : nt)
  {
  System.out.print(i+" ");
  }
System.out.println();
System.out.println("--------------------");   
  int[] temp=nt;     //new reference variable temp
     nt =new int[8];
     int j=0;
      for(int i : temp)
      {
      nt[j++]=i;          //copying data
      }
      for(int i : nt)
  {
  System.out.print(i+" ");
  }
}

}

Output: