我有一个格式数组
array
(
0 => array ( id = 1, name = chimpanzee, address = Singapore )
1 => array ( id = 2, name = meeting, address = USA )
2 => array ( id = 3, name = dynasty, address = Singapore )
3 => array ( id = 4, name = chocolate, address = Netherland )
4 => array ( id = 5, name = bananas, address = Singapore )
5 => array ( id = 6, name = fantasy, address = USA )
6 => array ( id = 7, name = football, address = England)
)
我想根据每个子阵列中的地址字段将其分组为一个新数组。
array
(
Singapore => array
(
0 => array ( id = 1, name = chimpanzee, address = Singapore )
2 => array ( id = 3, name = dynasty, address = Singapore )
4 => array ( id = 5, name = bananas, address = Singapore )
)
USA => array
(
1 => array ( id = 2, name = meeting, address = USA )
5 => array ( id = 6, name = fantasy, address = USA )
)
Netherland => array
(
3 => array ( id = 4, name = chocolate, address = Netherland )
)
England => array
(
6 => array ( id = 7, name = football, address = England)
)
)
答案 0 :(得分:0)
正如@ Rizier123所说,使用循环检查所有数组条目并将它们复制到一个新的(已排序)数组,如下所示:
import java.util.ArrayList;
public class Menu {
private ArrayList<String> meals;
public Menu() {
this.meals = new ArrayList<String>();
}
public void addMeal(String meal) {
if (!meals.contains(meal)) {
meals.add(meal);
}
}
public void printMeals() {
if (!meals.isEmpty())
System.out.println(this.meals);
}
public void clearMenu(){
meals.removeAll(meals);
}
}