Monday, 15 September 2014

java - Can someone please give an example for writing dynamic array without using ArrayList? -


I want to add element to the dynamically in the array. Can anyone help me on this?

int [] Data = New Int [10]; How can I create this array for dynamic entries?

I get this code from StakeVarf Flow. I think it can work.

** Public category DynamicArrayOfInt

{

Private int [] data;

Public DynamicAreOffInt () {

  data = new int [1];  

}

public int (status of int) {

  if (status> gt; data.length) return 0; Other data back [status]; } Enter the Zero (int position, int value) { 
  if (position> = data.length) {int newSize = 2 * Data .length; If (Position & gt; = New Size) newSize = 2 * Position; Int [] newData = new int [newSize]; System.arraycopy (data, 0, new data, 0, data. Length); Data = new data; System.out.println ("Increasing the Size of Dynamic Array" + New Size); } Data [status] = value;  

}} **

// system. Arrayopy (data, 0, new data, 0, data lamps);

You can think of the following simple arguments

  when The size of the array is a perfect integer [] = Arrays.copyOf (data, data. Length * 2);  

No comments:

Post a Comment