如何使用Java数组使用密钥字段



我正在尝试使用Java的数组使用键字段,但我对从哪里开始感到困惑。我的程序应该是用户输入一个名称,该名称将是唯一的标识符,并且所有这些信息都将显示与Mini Google的名称类型有关。我有两个课程,一个是我的抽象课,另一个是我的主要方法类。如果有人能帮助我,那将是很棒的,而且非常牢固。

这是我的班级。

import java.util.HashMap;
public class DataBase {
private  String name;
private  int year;
private  String status;
private  String habitat;
private  String lifespan;
public DataBase() {
}
HashMap list=new HashMap();
public DataBase(String name, int year, String status, String habitat, String lifespan) {
    this.name = name;
    this.year = year;
    this.status = status;
    this.habitat = habitat;
    this.lifespan = lifespan;
}

public  String getName() {
    return name;
}
public  void setName(String name) {
    this.name = name;
}
public  int getYear() {
    return year;
}
public  void setYear(int year) {
    this.year = year;
}
public  String getStatus() {
    return status;
}
public  void setStatus(String status) {
    this.status = status;
}

 public  String getHabitat() {
    return habitat;
}
public  void setHabitat(String habitat) {
    this.habitat = habitat;
   }

 public  String getLifeSpan() {
    return lifespan;
}
public  void setLifeSpan(String lifespan) {
   this.lifespan = lifespan;
}

    public String toString()
{
    String result = "";
    result += "nName: "+ getName() + "nYear: "+getYear()+ "nStatus: "+getStatus()+ "nHabitat: "+getHabitat()+"nLifeSpan: "+getLifeSpan();
    return result;
}
}

这是我的主要方法类。

import java.util.HashMap;
import java.util.Scanner;
public class DB
{
public static void main(String[] args) 
 {  
    Scanner in = new Scanner (System.in);
    String fName;
    System.out.print("Enter Name:");
    fName=in.next();
    String name;
    name=fName+" ";
    System.out.println(name);

    DataBase turtle1 = new DataBase("Diamondback terrapin", 1793, "Near Threatened","East Coast of US","8 years");
    DataBase turtle2 = new DataBase("Galápagos tortoise", 1535, "Vulnerable"," Galápagos Islands","At least 100 years");
    DataBase turtle3 = new DataBase("West African mud turtle", 1812, "Least Concern","West and Central Africa","25 years");
    DataBase turtle4 = new DataBase("Black softshell turtle", 1875, "Extinct in the Wild","South Asia","Unknown");    
    DataBase turtle5 = new DataBase("Radiated tortoise", 1802, "Critically Endangered","Southern Madagascar","At least 150 years");
    DataBase turtle6 = new DataBase("Painted terrapin", 1844, "Critically Endangered","East Asia","Unknown");
    DataBase turtle7 = new DataBase("Philippine pond turtle", 1920, "Critically Endangered ","Philippines","Unknown");
    DataBase turtle8 = new DataBase("Flattened musk turtle", 1955, "Critically Endangered","Southern US","20-50 years");
    DataBase turtle9 = new DataBase("McCord's box turtle", 1988, "Critically Endangered","China","Around 50 years");
    DataBase turtle10 = new DataBase("Leatherback sea turtle", 1761, "Vulnerable","Atlantic and Pacfic Oceans","Around 30 years");
    System.out.println(turtle1);
    HashMap<String,DataBase> turtles = new HashMap<>();
    turtles.put("Diamondback terrapin", new DataBase("Diamondback terrapin", 1793, "Near Threatened","East Coast of US","8 years"));
    turtles.put("Galapagos tortoise", new DataBase("Galapagos tortoise", 1535, "Vulnerable"," Galapagos Islands","At least 100 years"));
    turtles.put("West African mud turtle", new DataBase("West African mud turtle", 1812, "Least Concern","West and Central Africa","25 years"));
    turtles.put("Black softshell turtle", new DataBase("Black softshell turtle", 1875, "Extinct in the Wild","South Asia","Unknown"));
    turtles.put("Radiated tortoise", new DataBase("Radiated tortoise", 1802, "Critically Endangered","Southern Madagascar","At least 150 years"));
    turtles.put("Painted terrapin", new DataBase("Painted terrapin", 1844, "Critically Endangered","East Asia","Unknown"));
    turtles.put("Philippine pond turtle", new DataBase("Philippine pond turtle", 1920, "Critically Endangered ","Philippines","Unknown"));
    turtles.put("Flattened musk turtle", new DataBase("Flattened musk turtle", 1955, "Critically Endangered","Southern US","20-50 years"));
    turtles.put("McCord's box turtle", new DataBase("McCord's box turtle", 1988, "Critically Endangered","China","Around 50 years"));
    turtles.put("Leatherback sea turtle", new DataBase("Leatherback sea turtle", 1761, "Vulnerable","Atlantic and Pacfic Oceans","Around 30 years"));


    DataBase database = turtles.get("Diamondback terrapin");
    DataBase databas = turtles.get("Galapagos tortoise");
    DataBase databse = turtles.get("West African mud turtle");
    DataBase databa = turtles.get("Black softshell turtle");
    DataBase dataase = turtles.get("Radiated tortoise");
    DataBase datab = turtles.get("Painted terrapin");
    DataBase datase = turtles.get("Philippine pond turtle");
    DataBase datae = turtles.get("Flattened musk turtle");
    DataBase dataae = turtles.get("McCord's box turtle");
    DataBase datbase = turtles.get("Leatherback sea turtle");
    System.out.println(turtles.get(name));

}
}

更新:

我可以同时打印出名称和信息,但我不知道如何将两个连接在一起。

我认为您要做的是如下:

import java.util.HashMap;
public class DataEntry {
    private  String name;
    private  int year;
    public DataEntry(String name, int year) {
        this.name = name;
        this.year = year;
    }
    public  String getName() {  return name; }
    public  void setName(String name) { this.name = name;   }
    public  int getYear() { return year;}
    public  void setYear(int year) {    this.year = year;}
    @Override
    public String toString()
    {
        return "nName: "+ getName() + "nYear: "+getYear();
    }
    public static void main(String[] args)
     {
        //create 2 entries
        String name1 = "Diamondback terrapin", name2 = "Galápagos tortoise";
        DataEntry turtle1 = new DataEntry(name1, 1793);
        DataEntry turtle2 = new DataEntry(name2, 1535);
        //create collection and store entries
        DataCollection dc = new DataCollection();
        dc.addEntry(turtle1);
        dc.addEntry(turtle2);
        //retrieve an entry
        System.out.println(dc.getEntry(name1));
    }
}
class DataCollection{
    HashMap<String, DataEntry> list;
    DataCollection(){ list = new HashMap<>();   }
    void addEntry(DataEntry entry) {
        //todo verify input
        list.put(entry.getName(), entry);
    }
    DataEntry getEntry(String name) {
       //todo verify input          
       return list.get(name);
    }
    //todo add more methods as needed such as remove, get all entries
}

密钥与地图一起使用。

尝试用java.util.HashMap替换ArrayList

HashMap<String,DataBase> turtles = new HashMap<>();
turtles.put("Diamondback terrapin", new DataBase("Diamondback terrapin", 1793, "Near Threatened","East Coast of US","8 years"));
...
DataBase database = turtles.get("Diamondback terrapin");

最新更新