之前学习制作了DArray,了解ArrayList就容易了。

/**
 * 用于存储水果名及数量
 *
 */
public class Fruit{
    private String name;
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    private int count;

    public Fruit(String name,int count){
        this.name=name;
        this.count=count;
    }

}
import java.util.ArrayList;
import java.util.List;

public class Counter{
    private List<Fruit> ls;

    public Counter(){
        ls=new ArrayList<Fruit>();
    }

    public void add(String name){
        // 在链表中查找同名的fruit元素
        for(int i=0;i<ls.size();i++){
            if(ls.get(i).getName().equals(name)){
                // 找到了
                Fruit f=ls.get(i);
                // 增加同名水果的数量
                f.setCount(f.getCount()+1);

                return;
            }
        }

        // 没有找到,添加一个水果实例,数量为一
        ls.add(new Fruit(name,1));
    }

    public void printAll(){
        int sum=0;
        for(Fruit f:ls){
            sum+=f.getCount();
            System.out.println(f.getName()+"    "+f.getCount());
        }

        System.out.println("总计"+"    "+sum);
    }

    public static void main(String[] args){
        Counter c=new Counter();
        c.add("苹果");
        c.add("荔枝");
        c.add("荔枝");
        c.add("西瓜");
        c.printAll();
    }
}

爪哇国新游记之七----使用ArrayList统计水果出现次数的更多相关文章

  1. 爪哇国新游记之十三----XML文件读写

    /** * XML读写示例 * @author hx * */ public class XmlReaderWriter{ /** * 读取一个XML文件,返回一个雇员链表 * @param file ...

  2. 爪哇国新游记之二十二----排序判断重复时间复杂度为2n的位图法

    import java.util.ArrayList; import java.util.List; /** * 位图法 * 用于整型数组判重复,得到无重复列表 * */ public class B ...

  3. 爪哇国新游记之三十四----Dom4j的XPath操作

    Dom4j是Java访问XML的利器之一,另一个是JDom.记得当年因为粗掌握点JDomAPI但项目要求使用Dom4j还闹一阵情绪,现在看来真是没必要,只花一些时间成本就进去一个新世界绝对是值得做的一 ...

  4. 爪哇国新游记之十四----初试JDBC

    import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import ...

  5. 爪哇国新游记之二----用于计算三角形面积的Point类和TAngle类

    这次尝试用两个类完成一个面积计算任务: Point类代表平面上的点: public class Point { private float x; private float y; public Poi ...

  6. 爪哇国新游记之一----第一个类Cube

    将这个类作为Java学习的第一个类,简单易懂易上手. /** * 正方体类 */ public class Cube { private int length;// 正方体边长 private sta ...

  7. 爪哇国新游记之十九----使用Stack检查数字表达式中括号的匹配性

    /** * 辅助类 * 用于记载字符和位置 * */ class CharPos{ char c; int pos; public CharPos(char c,int pos){ this.c=c; ...

  8. 爪哇国新游记之二十九----访问URL获取输入流

    代码: import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileWriter; import ...

  9. 爪哇国新游记之二十八----从url指定的地址下载文件到本地

    package download; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; ...

随机推荐

  1. vue-music 关于基础组件 (Tab组件)

      定义在项目的基础组类别的 tab组件中,定义一个tab切换数量的数组 和一个currentIndex 当前高亮索引 的props,当前高亮(active)的类等于currentIndex === ...

  2. Codeforces 1 C. Ancient Berland Circus-几何数学题+浮点数求gcd ( Codeforces Beta Round #1)

    C. Ancient Berland Circus time limit per test 2 seconds memory limit per test 64 megabytes input sta ...

  3. Web开发中的路径问题总结

    原文地址:http://zzqrj.iteye.com/blog/806909 路径问题在Web开发中算是令人比较蛋疼的问题,尤其是用相对地址时,同样的代码,在不同的目录结构中竟然会出现有对有错的结果 ...

  4. python excellent code link

    1. Howdoi Howdoi is a code search tool, written in Python. 2. Flask Flask is a microframework for Py ...

  5. 洛谷——P1629 邮递员送信

    P1629 邮递员送信 题目描述 有一个邮递员要送东西,邮局在节点1.他总共要送N-1样东西,其目的地分别是2~N.由于这个城市的交通比较繁忙,因此所有的道路都是单行的,共有M条道路,通过每条道路需要 ...

  6. ES5 Object.create 方法

    Object.create(proto[, propertiesObject])The Object.create() method creates a new object with the spe ...

  7. AGC 014 B - Unplanned Queries

    题面在这里! 很显然的一件事是,我们把路径覆盖改成两个点分别到根的路径覆盖,答案是不会变的,因为lca以上被覆盖了两次不变奇偶性.. 这么做的好处就是,我们只需要考虑每个点的覆盖次数带来的影响就行了, ...

  8. HihoCoder - 1715 树的连通问题

    题面在这里! 正式告别文化课回归的第一题QWQ,然鹅半个月之后还是要退役QWQWQWQWQ 好像很久之前就见过的一个题,当时只会打一打 O(N^2) 的暴力QWQ,正好今天又写了遍这个暴力用来对拍23 ...

  9. 【高斯消元】CDOJ1785 曜酱的线性代数课堂(三)

    高斯消元求行列式板子. #include<cstdio> #include<cmath> #include<algorithm> #include<cstri ...

  10. winform窗体MaximizeBox

    如果MaximizeBox为false会导致Form2窗体底部不显示. =>解决办法TopMost属性为true. Form2 _frm2 = new Form2(); _frm2.Maximi ...