import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
public class mainClass
{
static Scanner keyboard = new Scanner(System.in);
static HashMap <Integer[], String> hMap;
public static void createAHashMap()
{
System.out.print("Express the initial capacity: ");
int initialCapacity = keyboard.nextInt();
System.out.print("Express the load factor: ");
float loadFactor = keyboard.nextFloat();
hMap = new HashMap <Integer[], String> (initialCapacity, loadFactor);
}
public static void insertProductInformations()
{
Integer values[] = new Integer[3];
System.out.print("\nEnter product's barcode number: ");
values[0] = keyboard.nextInt();
System.out.print("Enter product's name: ");
String productName = keyboard.next();
System.out.print("Enter product's amount: ");
values[1] = keyboard.nextInt();
System.out.print("Enter product's price: ");
values[2] = keyboard.nextInt();
hMap.put(values, productName);
}
public static void displayList()
{
Set set = hMap.entrySet();
Iterator iterator = set.iterator();
System.out.println("\nBarcode Number\tProduct Amount\tProduct Price\tProduct Name");
while (iterator.hasNext())
{
Map.Entry mEntry = (Map.Entry) iterator.next();
System.out.print(mEntry.getKey() + "\t\t" + mEntry.getValue() + "\n");
}
}
public static void main(String[] args)
{
createAHashMap();
System.out.print("\nEnter to add product '1'\n");
System.out.print("Enter to display products '2'\n");
System.out.print("Enter your choice: ");
int entry = keyboard.nextInt();
while (entry != -99)
{
if (entry == 1)
{
insertProductInformations();
}
if (entry == 2)
{
displayList();
}
System.out.print("\nExpress your new choice (Exit: -99): ");
entry = keyboard.nextInt();
}
}
}
你好!
我创建了一个哈希映射,其中包含超市中的产品信息。然而,我不能正确地显示我的键值(它是一个数组)。除了产品名称之外,它向我展示了无关的东西。我该如何纠正这个问题?
float loadFactor = keyboard.nextFloat(); hMap = new HashMap <Integer[], String> (initialCapacity, loadFactor);
中,是否有笔误?这意味着loadFactor
是float
类型,但你的哈希图存储了String
类型? - Azat Nugusbayev