Showing posts with label Array. Show all posts
Showing posts with label Array. Show all posts

Thursday, 28 April 2016

Ways to copy array in Java ? Which one is better ?

 There are  mainly  three ways : 

1) Using Loop

2) System.arrayCopy();

3) Arrays.copyOf() &&  Arrays.copyOfRange() 


    Lets go in brief with array copy.

1. Using Loop
 
  • We all know array start with index 0,so using loop you can initialize variable and put condition with array size like     array_name.length (Be remember its property not method).
  • Now iterate loop and copy one by one element into another array.
  •  Following is example for array Copy using for loop. 

  package com.my;
class Array_Loop{
public int[] copyArrayByLoop (int[] myArray){
int length = myArray.length;
int[] copy = new int [length];
for(int i=0;i<length;i++){
copy [i] = myArray[i];
}
return copy;
}
}


2. System.arrayCopy()

  • It depends on the virtual machine, but it looks as if it copies blocks of memory instead of copying single array elements. 
  • This would absolutely increase performance. It's always preferred over loops when performance is an issue.
  • System.arraycopy() uses JNI (Java Native Interface) to copy an array (or parts of it), so it is amazingly fast.

public static native void arraycopy(Object src,  int  srcPos,Object dest, int destPos,int length);

  • If dest is null, then a NullPointerException is thrown.
  • If src is null, then a NullPointerException is thrown and the destination array is not modified.
  • Otherwise, if any of the following is true, an ArrayStoreException is thrown and the destination is not modified:

  • The src argument refers to an object that is not an array.
  • The dest argument refers to an object that is not an array.
  • The src argument and dest argument refer to arrays whose component types are different primitive types.
  • The src argument refers to an array with a primitive component type and the dest argument refers to an array with a reference component type.
  • The src argument refers to an array with a reference component type and the dest argument refers to an array with a primitive component type.
  • Otherwise, if any of the following is true, an IndexOutOfBoundsException is thrown and the destination is not modified:
  • The srcPos argument is negative.
  • The destPos argument is negative.
  • The length argument is negative.
  • srcPos+length is greater than src.length, the length of the source array.
  • destPos+length is greater than dest.length, the length of the destination array.



 3. Arrays.copyOf() &&  Arrays.copyOfRange()

int []return  = Arrays.copyOf(int[] array,int newLength)


  • Copies the specified array, truncating or padding with false (if necessary) so the copy has the specified length. 
  • For all indices that are valid in both the original array and the copy, the two arrays will contain identical values. 
  • For any indices that are valid in the copy but not the original, the copy will contain false. 
  • Such indices will exist if and only if the specified length is greater than that of the original array.
  • Arrays.copyOf(T[], int) is easier to read. Internaly it uses System.arraycopy() which is a native call.
  • NegativeArraySizeException - if newLength is negative
  • NullPointerException - if array is null

byte []return  = Arrays.copyOfRange(byte[] array,int from,int to)

  • Copies the specified range of the specified array into a new array. 
  • The initial index of the range (from) must lie between zero and original.length, inclusive. 
  • The value at array[from] is placed into the initial element of the copy (unless from == original.length or from == to). 
  • Values from subsequent elements in the original array are placed into subsequent elements in the copy. The final index of the range (to), which must be greater than or equal to from, may be greater than original.length, in which case null is placed in all elements of the copy whose index is greater than or equal to original.length - from. 
  • The length of the returned array will be to - from.
  • ArrayIndexOutOfBoundsException - if from < 0 or from > original.lengt
  • IllegalArgumentException - if from > to
  • NullPointerException - if original is null

Saturday, 14 June 2014

An Array of Characters is Not a String

  • In the Java programming language, unlike C, an array of char is not a String, and neither a String nor an array of char is terminated by '\u0000' (the NULL character)
  • A String object is immutable, that is, its contents never change, while an array of char has mutable elements.
  • The method toCharArray in class String returns an array of characters containing the same character sequence as a String. The class StringBuffer implements useful methods on mutable arrays of characters.

Array Store Exception

For an array whose type is A[], where A is a reference type, an assignment to a component of the array is checked at run time to ensure that the value being assigned is assignable to the component.

If the type of the value being assigned is not assignment-compatible with the component type, an ArrayStoreException is thrown.



  • The variable pa has type Point[] and the variable cpa has as its value a reference to an object of type ColoredPoint[]. 
  • A ColoredPoint can be assigned to a Point; therefore, the value of cpa can be assigned to pa.
  • A reference to this array pa, for example, testing whether pa[1] is null, will not result in a run-time type error. 
  • This is because the element of the array of type ColoredPoint[] is a ColoredPoint, and every ColoredPoint can stand in for a Point, since Point is the superclass of ColoredPoint.
  • On the other hand, an assignment to the array pa can result in a run-time error. At compile time, an assignment to an element of pa is checked to make sure that the value assigned is a Point. 
  • But since pa holds a reference to an array of ColoredPoint, the assignment is valid only if the type of the value assigned at run time is, more specifically, a ColoredPoint. 
  • The Java Virtual Machine checks for such a situation at run time to ensure that the assignment is valid; if not, an ArrayStoreException is thrown.