java - Convert int, long, boolean, String, double in List (two variants), Set(two variants), Queue, Deque -
i have method convert array arraylist follows:
public class main { //https://docs.oracle.com/javase/tutorial/extra/generics/methods.html public static <t> void fromarraytocollection(t[] a, collection<t> c) { (t o : a) { c.add(o); } } public static void main(string[] args) { car [] cars = new car[2]; cars[0] = new car(1, "volvo", "2612axa"); cars[1] = new car(2, "toyota", "1861axa"); int [] = new int[2]; i[0] = 1; i[1] = 1; long [] l = new long[2]; l[0] = 122342141; l[1] = 214214211; double [] d = new double[2]; d[0] = 1.0; d[1] = 2.0; boolean [] b = new boolean[2]; b[0] = true; b[1] = false; string [] s = new string[2]; s[0] = "one"; s[1] = "two"; collection<string> sc = new arraylist<string>(); fromarraytocollection(s, sc); system.out.println(sc); } }
it easy convert other types of array. have class 3 fields.
public class car { int id; string car_name; string number; public car(int id, string car_name, string number) { this.id = id; this.car_name = car_name; this.number = number; } @override public string tostring() { return "car{" + "id=" + id + ", car_name='" + car_name + '\'' + ", number='" + number + '\'' + '}'; } }
how convert array cars collection ? , how convert collections arrays using iterator? thanks.
car[] carsarray = new car[2]; ... list<car> listofcars = arrays.aslist(carsarray);
Comments
Post a Comment