Tuesday Mar 06, 2007

Code Example: List.toArray(T[] a)

Most of the time, I write small code snippets and wrap them in a small test case to test my assumptions before I put them in a real places. For long a time, I wanted to capture these code snippets some where with my observations. Suddenly I realized, I could blog them too. I hope to continue this practice, but I am not very positive though :-)

Coming to my code snippet, I need to convert a List into an array and I saw java.util.List's " T[] toArray(T[] a)" fitting my bill. This syntax didn't really looked natural to me and I wrote this test case.

import java.util.ArrayList;
public class ToArray {
public static void main(String args[]) {
ArrayList list = new ArrayList();
list.add(1);
list.add(2);
list.add(3);
Integer[] ints = list.toArray(new Integer[]{});
for (Integer i : ints) {
System.out.println(i);
}
}
}

Output of theabove code snippet is:
$ java ToArray
1
2
3

I was bit confused whether the parameter to the toArray should be the same array
Integer[] ints = {};
ints = list.toArray(ints);
or any array of the same type
Integer[] ints = list.toArray(new Integer[]{});

Both of the above two works and what matters is only is the only type.

Tuesday Aug 01, 2006

Search option @ blogs.sun.com

Searching at blogs.sun.com doesn't seems to be returning correct results. I couldn't understand whether search is just applied to the content, or content and author, or title etc. For example, recently there were some posts on GTLC and when I tried to search I get zero results.

About

bsankararao

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today