首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在android中为另一个数组列表中的每个值创建数组列表

如何在android中为另一个数组列表中的每个值创建数组列表
EN

Stack Overflow用户
提问于 2017-03-25 20:22:38
回答 3查看 1.6K关注 0票数 0

我想创建一个应用程序来计算我的开支。我为trips创建了数组列表,现在我想为类别创建数组列表,为每个类别创建另一个数组列表。例如,在trip1中,我有三种类型: a、b、c。在每个类别中,我都有自己的支出。那么,我如何为trip数组列表中的每个值创建一个数组列表呢?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-03-25 21:40:29

您正在创建一个类似数据库的结构。以下是一些提示:

1)您不希望有重复的数据,因为在结构中引入不一致是非常容易的。

这意味着:不要同时在expanses列表和categories[cat]列表中添加扩展

2)也许,使用一些地图

3)使用一些类来聚合数据

我会这样做的:

代码语言:javascript
复制
import java.util.*;

public class Trip {

    /** An item in the expanses list */
    static class Item {
        final String item;
        final int cost;
        public Item(String item, int cost) {
            this.item = item;
            this.cost = cost;
        }
        @Override public String toString() {
            return this.item + " (" + this.cost + "$)";
        }
    }

    /** A map with category as key and the associed list of items as value */
    Map<String,List<Item>> expanses;


    public Trip() {
        this.expanses = new HashMap<String,List<Item>>();
        String[] categories = {"food","clothes","souvenir"};
        for (String cat: categories) { // init the categories with empty lists
            this.expanses.put(cat, new ArrayList<Item>());
        }
    }



    /** Register a new expanse to the trip. */
    public void add(String item, int cost, String category) {
        List<Item> list = this.expanses.get(category);
        if (list == null)
            throw new IllegalArgumentException("Category '"+category+"' does not exist.");
        list.add( new Item(item, cost) );
    }

    /** Get the expanses, given a category.
     * @return  a fresh ArrayList containing the category elements, or null if the category does not exists
     */
    public List<Item> getItems(String category) {
        List<Item> list = this.expanses.get(category);
        if (list == null)
            return null;
        return new ArrayList<Item>(list);
    }

    /** Get the expanses, given a category.
     * @return  a fresh ArrayList containing all the elements
     */
    public List<Item> getItems() {
        List<Item> list = new ArrayList<Item>();
        for (List<Item> l: this.expanses.values()) // fill with each category items
            list.addAll(l);
        return list;
    }

    /** Get the total cost, given a category. */
    public int getCost(String category) {
        List<Item> list = this.expanses.get(category);
        if (list == null)
            return -1;
        int cost = 0;
        for (Item item: list)
            cost += item.cost;
        return cost;
    }

    /** Get the total cost. */
    public int getCost() {
        int cost = 0;
        for (List<Item> l: this.expanses.values())
            for (Item item: l)
                cost += item.cost;
        return cost;
    }


    // lets test this structure
    public static void main(String[] args) {

        Trip trip1 = new Trip();

        trip1.add("apple", 10, "food");
        trip1.add("cheese", 15, "food");
        trip1.add("hat", 30, "clothes");


        System.out.println( trip1.getItems("food") );
        System.out.println( trip1.getCost("food") );
        System.out.println();
        System.out.println( trip1.getItems() );
        System.out.println( trip1.getCost() );

    }

}
票数 0
EN

Stack Overflow用户

发布于 2017-03-25 21:12:24

你需要上几节课

代码语言:javascript
复制
class Trip {
    private ArrayList[Category] categories = new ArrayList();

    public void addCategory(Category c) {
         categories.add(c);
    }
    public void getCategory(int index) {
         categories.get(index);
    }
}

class Category {
    private ArrayList[Expense] expenses = new ArrayList();

    public void addExpense(Expense e) {
         expenses.add(e);
    }
}


class Expense {
    private String name;
    private int amount;

    public Expense(name, amount) {
        this.name = name;
        this.amount = amount;
    }
}

然后,在代码的其他部分中,您可以执行以下操作

代码语言:javascript
复制
Trip africa = new Trip();
africa.addCategory(new Category());
africa.getCategory(0).addExpense(new Expense("apple", 10));

您可能希望ArrayList的类别在Trip中是一个HashList,然后您的类别可以有名称。

票数 1
EN

Stack Overflow用户

发布于 2017-03-25 20:53:03

尝试将一次旅行作为一个对象(与其相关的值),然后将其放入一个列表中,而不是列出这么多列表。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43021546

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档