How to filter an array in Java? -
how can filter array in java?
i have array of objects, example cars:
class:
public class car{ public int doors; public car(int d){ this.doors = d; } }
use:
car [] cars = new cars[4]; cars[0] = new car(3); cars[1] = new car(2); cars[2] = new car(4); cars[3] = new car(6);
now want filter array of cars, keeping 4 doors , more:
for(int = 0; i<cars.length; i++){ if(cars[i].doors > 4) //add cars[i] new array } }
how should this?
before did vector:
vector subset = new vector(); for(int = 0; i<cars.length; i++){ if(cars[i].doors > 4) //add cars[i] new array subset.addelement(cars[i]); } }
and make new array size of vector. loop on vector again , fill new array. know large procedure simple.
i'm using j2me.
edit: saw arraylist not in j2me, based on documentation, have vector. if vector class different j2se vector (as this documentation indicates), perhaps following code work:
vector carlist = new vector(); for(int = 0; i<cars.length; i++){ if(cars[i].doors > 4) carlist.addelement(cars[i]); } } car[] cararray = new car[carlist.size()]; carlist.copyinto(cararray);
Comments
Post a Comment