Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java—使HashMap从其他类可用的问题_Java_Hashmap - Fatal编程技术网

Java—使HashMap从其他类可用的问题

Java—使HashMap从其他类可用的问题,java,hashmap,Java,Hashmap,我已经研究了很多关于当前问题的答案,但是我不确定到底出了什么问题: import java.util.Map; import java.util.HashMap; public class Items { public static void main (String args[]) { HashMap<String, Double> Hallway = new HashMap<String, Double&

我已经研究了很多关于当前问题的答案,但是我不确定到底出了什么问题:

    import java.util.Map;
    import java.util.HashMap;

    public class Items
    {
      public static void main (String args[])
      {
      HashMap<String, Double> Hallway = new HashMap<String, Double>();
      HashMap<String, Double> Toilet = new HashMap<String, Double>();
      HashMap<String, Double> ChemistryLab = new HashMap<String, Double>();
      HashMap<String, Double> Outdoors = new HashMap<String, Double>();
      HashMap<String, Double> Library = new HashMap<String, Double>();
      HashMap<String, Double> Engineering = new HashMap<String, Double>();
      HashMap<String, Double> Cafeteria = new HashMap<String, Double>();
      HashMap<String, Double> ComputerLab = new HashMap<String, Double>();
      HashMap<String, Double> LectureTheater = new HashMap<String, Double>();
      HashMap<String, Double> MedicalCentre = new HashMap<String, Double>();
      }

    public HashMap<String, Double> getHallwayItems() 
    {
      return Hallway;        
    }

    public HashMap<String, Double> getToiletItems() 
    {
      return Toilet;
    }

    public HashMap<String, Double> getChemistryLabItems() 
    {
     return ChemistryLab;
    }

    public HashMap<String, Double> getOutdoorItems() 
    {
     return Outdoors;
    }

    public HashMap<String, Double> getLibraryItems() 
    {
     return Library;
    }

    public HashMap<String, Double> getEngineeringItems() 
    {
     return Engineering;
    }

    public HashMap<String, Double> getCafeteriaItems() 
    {
     return Cafeteria;
    }

    public HashMap<String, Double> getComputerLabItems() 
    {
     return ComputerLab;
    }

    public HashMap<String, Double> getLectureTheaterItems() 
    {
     return LectureTheater;
    }

    public HashMap<String, Double> getMedicalCentreItems() 
    {
     return MedicalCentre;
    }

    }
import java.util.Map;
导入java.util.HashMap;
公共类项目
{
公共静态void main(字符串参数[])
{
HashMap Hallway=新建HashMap();
HashMap厕所=新建HashMap();
HashMap ChemistryLab=新的HashMap();
HashMap Outdoors=新建HashMap();
HashMap库=新建HashMap();
HashMap Engineering=新的HashMap();
HashMap自助餐厅=新建HashMap();
HashMap ComputerLab=新HashMap();
HashMap讲师水=新HashMap();
HashMap MedicalCenter=新HashMap();
}
公共HashMap getHallwayItems()
{
回程走廊;
}
公共HashMap getToileItems()
{
返回厕所;
}
公共HashMap getChemistryLabItems()
{
返回化学标签;
}
公共HashMap getOutdoorItems()
{
返回户外;
}
公共HashMap getLibraryItems()
{
返回库;
}
公共HashMap getEngineeringItems()
{
返回工程;
}
公共HashMap getcaffeetaitems()
{
返回自助餐厅;
}
公共HashMap getComputerLabItems()
{
返回计算机实验室;
}
公共HashMap Get讲师etheaterItems()
{
还水;
}
公共HashMap GetMedicalCenter项目()
{
返回医疗中心;
}
}
它说在我尝试编译时找不到变量Hallway,但是,我不知道如何解决这个问题。
感谢您提供的帮助。

您需要将Hallway和others定义为实例变量。这些变量是在方法范围中定义的,在其他方法中无法访问

import java.util.Map;
import java.util.HashMap;

public class Items
{
  HashMap<String, Double> Hallway = new HashMap<String, Double>();
  HashMap<String, Double> Toilet = new HashMap<String, Double>();
  HashMap<String, Double> ChemistryLab = new HashMap<String, Double>();
  HashMap<String, Double> Outdoors = new HashMap<String, Double>();
  HashMap<String, Double> Library = new HashMap<String, Double>();
  HashMap<String, Double> Engineering = new HashMap<String, Double>();
  HashMap<String, Double> Cafeteria = new HashMap<String, Double>();
  HashMap<String, Double> ComputerLab = new HashMap<String, Double>();
  HashMap<String, Double> LectureTheater = new HashMap<String, Double>();
  HashMap<String, Double> MedicalCentre = new HashMap<String, Double>();
  public static void main (String args[])
  {

  }

public HashMap<String, Double> getHallwayItems() 
{
  return Hallway;        
}

public HashMap<String, Double> getToiletItems() 
{
  return Toilet;
}

public HashMap<String, Double> getChemistryLabItems() 
{
 return ChemistryLab;
}

public HashMap<String, Double> getOutdoorItems() 
{
 return Outdoors;
}

public HashMap<String, Double> getLibraryItems() 
{
 return Library;
}

public HashMap<String, Double> getEngineeringItems() 
{
 return Engineering;
}

public HashMap<String, Double> getCafeteriaItems() 
{
 return Cafeteria;
}

public HashMap<String, Double> getComputerLabItems() 
{
 return ComputerLab;
}

public HashMap<String, Double> getLectureTheaterItems() 
{
 return LectureTheater;
}

public HashMap<String, Double> getMedicalCentreItems() 
{
 return MedicalCentre;
}

}
import java.util.Map;
导入java.util.HashMap;
公共类项目
{
HashMap Hallway=新建HashMap();
HashMap厕所=新建HashMap();
HashMap ChemistryLab=新的HashMap();
HashMap Outdoors=新建HashMap();
HashMap库=新建HashMap();
HashMap Engineering=新的HashMap();
HashMap自助餐厅=新建HashMap();
HashMap ComputerLab=新HashMap();
HashMap讲师水=新HashMap();
HashMap MedicalCenter=新HashMap();
公共静态void main(字符串参数[])
{
}
公共HashMap getHallwayItems()
{
回程走廊;
}
公共HashMap getToileItems()
{
返回厕所;
}
公共HashMap getChemistryLabItems()
{
返回化学标签;
}
公共HashMap getOutdoorItems()
{
返回户外;
}
公共HashMap getLibraryItems()
{
返回库;
}
公共HashMap getEngineeringItems()
{
返回工程;
}
公共HashMap getcaffeetaitems()
{
返回自助餐厅;
}
公共HashMap getComputerLabItems()
{
返回计算机实验室;
}
公共HashMap Get讲师etheaterItems()
{
还水;
}
公共HashMap GetMedicalCenter项目()
{
返回医疗中心;
}
}

走廊
是主函数内部的局部变量。main也是一个静态函数。在
main
中声明的这些变量不能从其他函数访问

有两种选择

1) 将所有映射声明为对象的静态成员,并使用static
getHallway()
static
getToilet
等。 2) 我个人建议,

导入java.util.Map; 导入java.util.HashMap

public class Items
{
    private HashMap<String, Double> Hallway = new HashMap<String, Double>();
    private HashMap<String, Double> Toilet = new HashMap<String, Double>();
    ...

    public HashMap<String, Double> getHallwayItems() 
    {
       return Hallway;        
    }
    ...
    ...

    public static void main (String args[])
    {
        Items myItem = new Items();
         myItem.getHallwayItems(); // and do whatever you want.
    }
}
公共类项目
{
private HashMap Hallway=new HashMap();
私有HashMap厕所=新建HashMap();
...
公共HashMap getHallwayItems()
{
回程走廊;
}
...
...
公共静态void main(字符串参数[])
{
Items myItem=新项目();
myItem.getHallwayItems();//并执行任何您想要的操作。
}
}

这样,我们还可以利用OOPS…

您需要了解和了解标准Java命名约定。