java中hashmap的应用,本人小白,这题要咋搞

java中hashmap的应用,本人小白,这题要咋搞,第1张

import java.util.HashMap

import java.util.Map

public class work {

    public static void main(String[] args) {

        class Student { //定义内部类Student

            private String num //学号

            private String name//名称

            private int age//年龄

            Student() {

            } //无参构造方法

            Student(String num, String name, int age) { //有参构造方法

                this.num = num

                this.name = name

                this.age = age

            }

            public String getNum() {

                return num

            }

            public void setNum(String num) {

                this.num = num

            }

            public String getName() {

                return name

            }

            public void setName(String name) {

                this.name = name

            }

            public int getAge() {

                return age

            }

            public void setAge(int age) {

                this.age = age

            }

            @Override

            public String toString() { //复写toString方法

                return "学号:"+this.num+" , 姓名:"+this.name+" , 年龄:"+this.age

            }

        }

        Map<String, Student> staff = new HashMap<String, Student>()

        Student student1 = new Student("15H001", "唐僧", 40) //用有参构造方法 创建一个学生

        Student student2 = new Student()//用无参构造方法 创建一个学生

        student2.setNum("15H002")

        student2.setName("孙悟空")

        student2.setAge(2000)

        Student student3 = new Student("15H003", "猪八戒", 1000)//用有参构造方法 创建一个学生

        staff.put(student1.getNum(), student1) //1号学生放入hashMap

        staff.put(student2.getNum(), student2)//2号学生放入hashMap

        staff.put(student3.getNum(), student3)//3号学生放入hashMap

        System.out.println("3.根据 key(学号)查找学号为 15H003 和 15H004 的学生,如果存在则输出其学号、姓名、年龄信息,否则输出相应的提示信息")

        String[] nums = {"15H003", "15H004"}

        for (int i = 0 i < nums.length i++) {

            System.out.println("查找学号:" + nums[i])

            Student student = staff.get(nums[i])

            if (student != null) { //如果找到了

                System.out.println("学号:" + student.getNum() + " , 姓名:" + student.getName() + " , 年龄:" + student.getAge())

            } else { //如果没有找到

                System.out.println("学号:" + nums[i] + "的学生没有擦寻到相关信息")

            }

        }

        System.out.println("4.输出所有学生的信息")

        for (Map.Entry<String, Student> entry : staff.entrySet()) {

            String key = entry.getKey()

            Student value = entry.getValue()

            System.out.println("学号:" + key + " , 姓名:" + value.getName() + " , 年龄:" + value.getAge())

        }

        System.out.println("5.移除唐僧")

        staff.remove("15H001") //根据唐僧的学号移除唐僧

        System.out.println("6.把 HashMap 变成数组,并输出每个数组元素的信息(学号、姓名、年龄)")

        Object[] values = staff.values().toArray()

        for (int i = 0 i < values.length i++) {

            Student student = (Student)values[i]

            System.out.println(student.toString())

        }

    }

}

你可以这样创建一个商品类:

class Student {

private int id

private String name

private int age

// 其他的属性

public Student(int id, String name, int age) {

this.id = id

this.name = name

this.age = age

}

// getters and setters

}

然后,你可以使用Java的HashMap类来管理学生信息:

import java.util.HashMap

class StudentManager {

private HashMap<Integer, Student>students

public StudentManager() {

students = new HashMap<>()

}

public void addStudent(Student student) {

students.put(student.getId(), student)

}

public Student getStudent(int id) {

return students.get(id)

}

public void removeStudent(int id) {

students.remove(id)

}

}

这样,你就可以使用StudentManager类来管理学生信息了。例如,你可以使用addStudent方法来添加学生信息,使用getStudent方法来获取学生信息,使用removeStudent方法来删除学生信息。

分类: 电脑/网络 >>程序设计 >>其他编程语言

问题描述:

要用hashMap方法,我一用PUT就出错,帮看看哪里有错啊;最好讲讲HASHMAP的用法import javax.swing.*import java.util.*public class StudetHashMap{public static void main(String args[]){HashMap stu=new HashMap()System.out.println ("欢迎进入学生系统")System.out.println ("1.添加学生")System.out.println ("2.查询学生")System.out.println ("3.删除学生")int c=0do{int choice=Integer.parseInt(JOptionPane.showInputDialog(null,"请输入你的选择"))switch(choice){case 1:Student newstu=nullString code=JOptionPane.showInputDialog(null,"请你输入学号")String name=JOptionPane.showInputDialog(null,"请你输入姓名")newstu=new Student(code,name)breakcase 2:String codes=JOptionPane.showInputDialog(null,"请你输入学号")Student temp=nullfor (int i = 0i<stu.size()i++){temp=(Student)stu.get(i)if(temp.getCode().equals(codes)){break}}System.out.println (temp.getName())breakcase 3:String codee=JOptionPane.showInputDialog(null,"请输入学号")Student temps=nullint i=0for ( i = 0i<stu.size()i++){temps=(Student)stu.get(i)if(temps.getCode().equals(codee)){break}}stu.remove(i)}c=Integer.parseInt(JOptionPane.showInputDialog(null,"继续吗?1.继续\n2.不继续"))}while(c==1)}}class Student{private String nameprivate String codepublic Student(String code,String name){this.name=namethis.code=code}public void setCode(String code){this.code=code}public void setName(String name){this.name=name}public String getName(){return name}public String getCode(){return code}}

解析:

import javax.swing.*

import java.awt.*

import java.awt.event.*

import java.util.*

import .bruceeckel.swing.*

public class TrackEvent extends JApplet {

private HashMap h = new HashMap()

private String[] event = {

"focusGained", "focusLost", "keyPressed",

"keyReleased", "keyTyped", "mouseClicked",

"mouseEntered", "mouseExited", "mousePressed",

"mouseReleased", "mouseDragged", "mouseMoved"

}

private MyButton

b1 = new MyButton(Color.BLUE, "test1"),

b2 = new MyButton(Color.RED, "test2")

class MyButton extends JButton {

void report(String field, String msg) {

((JTextField)h.get(field)).setText(msg)

}

FocusListener fl = new FocusListener() {

public void focusGained(FocusEvent e) {

report("focusGained", e.paramString())

}

public void focusLost(FocusEvent e) {

report("focusLost", e.paramString())

}

}

KeyListener kl = new KeyListener() {

public void keyPressed(KeyEvent e) {

report("keyPressed", e.paramString())

}

public void keyReleased(KeyEvent e) {

report("keyReleased", e.paramString())

}

public void keyTyped(KeyEvent e) {

report("keyTyped", e.paramString())

}

}

MouseListener ml = new MouseListener() {

public void mouseClicked(MouseEvent e) {

report("mouseClicked", e.paramString())

}

public void mouseEntered(MouseEvent e) {

report("mouseEntered", e.paramString())

}

public void mouseExited(MouseEvent e) {

report("mouseExited", e.paramString())

}

public void mousePressed(MouseEvent e) {

report("mousePressed", e.paramString())

}

public void mouseReleased(MouseEvent e) {

report("mouseReleased", e.paramString())

}

}

MouseMotionListener mml = new MouseMotionListener() {

public void mouseDragged(MouseEvent e) {

report("mouseDragged", e.paramString())

}

public void mouseMoved(MouseEvent e) {

report("mouseMoved", e.paramString())

}

}

public MyButton(Color color, String label) {

super(label)

setBackground(color)

addFocusListener(fl)

addKeyListener(kl)

addMouseListener(ml)

addMouseMotionListener(mml)

}

}

public void init() {

Container c = getContentPane()

c.setLayout(new GridLayout(event.length + 1, 2))

for(int i = 0i <event.lengthi++) {

JTextField t = new JTextField()

t.setEditable(false)

c.add(new JLabel(event[i], JLabel.RIGHT))

c.add(t)

h.put(event[i], t)

}

c.add(b1)

c.add(b2)

}

public static void main(String[] args) {

Console.run(new TrackEvent(), 700, 500)

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/11519401.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-16
下一篇2023-05-16

发表评论

登录后才能评论

评论列表(0条)

    保存