How to work with arrays

Search for: java initialize an array


private int[] xarray = null;
private int[] yarray = null;
private String[] stringArray = null;

int sides = 6;
xarray = new int[sides];
yarray = new int[sides+2];

More on arrays


xarray = new int[]{1,2,3,4,5,6};

int sides = 6;
xarray = new int[sides]{1,2,3,4,5,6};

How do you print an array in java

Search for: How do you print an array in java


private void printArray(float array[])
{
   StringBuilder sb = new StringBuilder("array:");
   for(int i=0;i<array.length;i++)
   {
      sb.append(";").append(array[i]);
   }
   Log.d("hh",sb.toString());
}

Sample code for arrays

jdk list to array sample code

Search for: jdk list to array sample code

Java 7 docs on toArray


public String[] getColumnNames()
{
   List<String> cols = new ArrayList<String>();
   populateYourColumnNames(cols);

   String[] columnNamesArray = new String[cols.size()];
   return cols.toArray(columnNamesArray);
}

Yes. it is really weird that the toArray() function takes an array to populate and also returns the same object. Internally it may even choose to new a brand new array if the size is less!