Okay I am trying to get an array (a primitive integer array) called a1 which contains integers. I want to return an ArrayList that has the entries of a1? This should be really simple, but it is just one of those days....


public static ? formlist(//a1 is sent as a parameter so int a1[] ?)


For example you can do like this. It all depends on your requirments.

[B]import[/B] java.util.ArrayList;

public [B]class[/B] ArrayPass {[INDENT]ArrayList<Integer> arraylist = [B]new[/B] ArrayList<Integer>();[/INDENT]

[INDENT][B]public[/B] ArrayList<Integer> MyArrayList([B]int[/B] [] a1)[/INDENT]


[INDENT][INDENT][B]for[/B](Integer a : a1)[/INDENT][INDENT]{[INDENT]arraylist.add(a);[/INDENT][INDENT]System.[I]out[/I].println(a);[/INDENT]}
[B]return[/B] arraylist;



[INDENT][B]public[/B] [B]static[/B] [B]void[/B] main(String [] args)[/INDENT][INDENT]{[INDENT]ArrayPass ap = [B]new[/B] ArrayPass(); [/INDENT][INDENT]ArrayList<Integer> a = [B]new[/B] ArrayList<Integer>();[/INDENT][INDENT][B]int[/B] [] a1 = {1,2,3,4,5};[/INDENT][INDENT]a = ap.MyArrayList(a1);[/INDENT][INDENT][B]for[/B](Integer b : a)[INDENT]{[/INDENT][INDENT]System.[I]out[/I].println(" after passing : "+ b);[/INDENT][INDENT]}[/INDENT]


This article has been dead for over six months. Start a new discussion instead.