How to work with arrays

satya - Friday, June 26, 2009 3:04:35 PM

java initialize an array

Search for: java initialize an array

satya - Friday, June 26, 2009 3:06:52 PM

declaring a set of int arrays


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

satya - Friday, June 26, 2009 3:07:54 PM

Initializing them in a function


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

satya - Friday, June 26, 2009 3:10:00 PM

More on arrays

More on arrays

satya - Friday, June 26, 2009 3:12:15 PM

Intializing them with values


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

satya - Friday, June 26, 2009 3:35:17 PM

but the following would be wrong


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

satya - Friday, June 26, 2009 3:35:41 PM

How do you print an array in java

How do you print an array in java

Search for: How do you print an array in java

satya - Friday, June 26, 2009 3:38:16 PM

If nothing else is possible


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());
}

satya - Friday, June 26, 2009 3:52:03 PM

Sample code for arrays

Sample code for arrays

satya - 8/29/2014 9:59:17 AM

jdk list to array sample code

jdk list to array sample code

Search for: jdk list to array sample code

satya - 8/29/2014 10:01:53 AM

Java 7 docs on toArray

Java 7 docs on toArray

satya - 8/29/2014 1:30:56 PM

You can do this


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!