免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1184 | 回复: 0
打印 上一主题 下一主题

Java数组与数据结构(2) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-03-18 15:13 |只看该作者 |倒序浏览
Java数组与数据结构
存储对象
通常我们存储的数据记录是许多字段的集合。在java中一条数据记录经常由一个类对象来表示。例如可以构造一个典型的类来存储一个公司的职员数据。
public class Person {
    private String lastName;
    private String firstName;
    private int age;
    /** Creates a new instance of Person */
    void Person(String last, String first, int a) {
        lastName=last;
        firstName=first;
        age=a;
    }
    void displayPerson() {
        System.out.print("  Last name: "+lastName);
        System.out.print(", Firstname: "+firstName);
        System.out.print(",  Age: "+age);
    }
    public String getLast() {
        return lastName;
    }
}
构造函数创建一个新的Person对象并将它的各个字段初始化。DisplayPerson方法显示了一个Person对象的数据。GetLast返回Person的姓;这是用于搜索所需的关键字字段。
public class Person {
   
    private String lastName;
   
    private String firstName;
   
    private int age;
   
    /** Creates a new instance of Person */
   
    public Person(String last, String first, int a) {
        lastName=last;
        firstName=first;
        age=a;
    }
   
    public void displayPerson() {
        System.out.print("  Last name: "+lastName);
        System.out.print(", Firstname: "+firstName);
        System.out.println(",  Age: "+age);
    }
   
    public String getLast() {
        return lastName;
    }
   
}
public class ClassDataApp {
   
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        int maxSize=100;
        ClassDataArray arr;
        arr=new ClassDataArray(maxSize);
        
        arr.insert("Evans", "Patty",24);
        arr.insert("Smith","Lorraine",37);
        arr.insert("Yee","Tom", 43);
        arr.insert("Adams", "Henry", 63);
        arr.insert("Hashimoto","Sato",21);
        arr.insert("Stimson","Henry",29);
        arr.insert("Velasquez", "Jose", 72);
        arr.insert("Lamarque","Henry", 54);
        arr.insert("Vang", "Minh",22);
        arr.insert("Creswell","Lucinda",18);
        
        arr.displayA();
        
        String searchKey="Stimson";
        Person found;
        found=arr.find(searchKey);
        if(found!=null)
        {
            System.out.print("Found ");
            found.displayPerson();
        }
        else
            System.out.println("Can't find "+searchKey);
        
        System.out.println("Deleting Smith,Yee,and Creswell");
        arr.delete("Smith");
        arr.delete("Yee");
        arr.delete("creswell");
        arr.displayA();
    }
   
}


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/1925/showart_16369.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP