Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/395.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 [TreeMap]:在一个类中创建和加载TreeMap,并从几个单独/不同的类访问该Tmap_Java_Get_Put_Treemap_Bluej - Fatal编程技术网

Java [TreeMap]:在一个类中创建和加载TreeMap,并从几个单独/不同的类访问该Tmap

Java [TreeMap]:在一个类中创建和加载TreeMap,并从几个单独/不同的类访问该Tmap,java,get,put,treemap,bluej,Java,Get,Put,Treemap,Bluej,编译错误由“新”TMap定义解决,而不是可能的重复引用。我想说清楚。我是Java新手,这是我的第一篇帖子 问题是,正如当时一样,无法从不同的类访问我的TMap 话虽如此,我的看跌期权由于随后的“新”Get而丢失。我似乎无法访问TMap而不反复重新创建它。 事件顺序:Define/init TMap=>Put/Get to TMap=>Get TMap with“new” 我在Stackoverflow问题中发现了一些类似的好主意:“从不同的类访问HashMap” 创建并初始化我的树映射“Data

编译错误由“新”TMap定义解决,而不是可能的重复引用。我想说清楚。我是Java新手,这是我的第一篇帖子

问题是,正如当时一样,无法从不同的类访问我的TMap

话虽如此,我的看跌期权由于随后的“新”Get而丢失。我似乎无法访问TMap而不反复重新创建它。 事件顺序:Define/init TMap=>Put/Get to TMap=>Get TMap with“new”

我在Stackoverflow问题中发现了一些类似的好主意:“从不同的类访问HashMap”

创建并初始化我的树映射“DataStorage”和Put/Get“PutDataStorage”类工作正常(成功加载并能够获取这些记录)

我尝试使用“新”数据存储进行Get(因为我还没有想到如何以其他方式访问该表),但失败了,因为我创建了一个新的Tmap

我在这些例子中遗漏了一些东西。也许一个完整的例子可以帮助我,而不是碎片。我喜欢这些作品的概念,但是,我太新了,现在无法欣赏单个作品,并且已经在这方面花费了太多的时间(断断续续的几周)

是的,我是新来的;)。我总有一天会把它修好的

总结:

理想情况下,我试图完成的是在一个类中创建并加载我的树映射。从一个或多个单独的/附加的/其他类访问相同的树映射

我现在拥有的:丢失了最初加载的数据和地图。无法从其他类访问我的树映射

与此同时,我还在尝试不同的想法,玩软件包等等

定义/初始化树映射:数据存储

import java.util.TreeMap;
public class DataStorage {
 public static TreeMap<String, Integer> people = new TreeMap<String, Integer>();
}

import java.util.TreeMap;
public class PutDataStorage {
/** 
 * Run one time "put"
 */
    public static void main(String[] args) {  
       DataStorage storage = new DataStorage();
       TreeMap<String, Integer> people = storage.people;
            people.put("bob", 2);
            people.put("susan", 5);

        System.out.println("PutData whole Entry  " +people.entrySet());
        System.out.println("PutData First Entry  " +people.firstEntry());
        System.out.println("PutData susan Value  " +people.get("susan"));
    }
    }

输出:来自Get

GetData 1stEntry  popeye=3
GetData bobValue  null
GetData popValue  3
GetData lowerKey  popeye=3
GetData hireKey   popeye=3

预期的Get结果应为:

GetData 1stEntry  bob=2
GetData bobValue  2
GetData popValue  3
GetData lowerKey  bob=2
GetData highKey   susan=5


提前感谢您。

这两门课都是“静态”的

无论是否创建公共定义的包,这都是有效的。我创建并测试了两者。如果需要,请注释掉两个类中的包名

请注意,我以非升序排列put,以证明导航对树映射有效

不要因为回答行的数量而惊慌,因为大多数都是打印和评论

PutDS类(创建并加载树映射及其记录)

//第二包wherewego;
导入java.util.TreeMap;
公共类PutDS{//PutDS(数据存储)
公共静态树映射gettMapCashType(){System.out.println(“PutDS-entered”);
//创建TreeMap=tMapCashType并将两条记录“放入”tMapCashType
TreeMap tMapCashType=新TreeMap();
tMapCashType.put(“C”,“信用卡”);//故障puts
tMapCashType.put(“A”、“全部”);
System.out.println(“PutDS”+tMapCashType);
返回tMapCashType;
}
}
“main”类执行PutDS,然后放入额外的TreeMap记录,然后使用TreeMap导航关键字获取大量记录:

//package twowherewego;
import java.util.TreeMap;

public class GetDS{         // Get DS(Data Storage)

    public static void main(String[] args) {  
        System.out.println("Works with or without 'towherewego' package cause it is public");
        System.out.println("** GetDS main: call PutDS and...");
        // Execute PutDS with method=gettMapCashType() 
       TreeMap<String, String> people = PutDS.gettMapCashType();   //class=PutDS "." method
       System.out.println("** back into GetDS main: after call to PutDS ");
                                            // space or no space ='s no difference
       people.put("D","popeye");            // notice this "put" has no space                                            
       people.put("B", "Bank Card");        // notice this "put" has a space after the ","

        System.out.println("{{{ Navigational helpful keywords, etc }}}" );
        System.out.println("GetData keySet      " +people.keySet());         // key 
        System.out.println("GetData entrySet    " +people.entrySet());       // key and value 
        System.out.println("GetData 1stEntry    " +people.firstEntry());     // 1st entry/key 
        System.out.println("GetData B_BankCard  " +people.get("B"));         // get B value 
        System.out.println("GetData B_lowerKey  " +people.lowerEntry("B"));  // get B lower
        System.out.println("GetData B_highrKey  " +people.higherEntry("B")); // get B higher    
        System.out.println("GetData lastEntry   " +people.lastEntry());      // last entry/key

           System.out.println("**  << End of GetDS >>");
    }
    }         

//第二包wherewego;
导入java.util.TreeMap;
公共类GetDS{//Get DS(数据存储)
公共静态void main(字符串[]args){
System.out.println(“使用或不使用'ToWhere Wego'包,因为它是公共的”);
System.out.println(“**GetDS main:call PutDS and…”);
//使用方法=gettMapCashType()执行PutDS
TreeMap people=PutDS.gettMapCashType();//class=PutDS“。方法
System.out.println(“**返回GetDS main:在调用PutDS之后”);
//空间还是没有空间没有区别
people.put(“D”,“大力水手”);//注意这个“put”没有空格
people.put(“B”,“银行卡”);//注意这个“put”在“,”后面有一个空格
System.out.println(“{{{{导航有用关键字等}}”);
System.out.println(“GetData keySet”+people.keySet());//key
System.out.println(“GetData entrySet”+people.entrySet());//键和值
System.out.println(“GetData 1stEntry”+people.firstEntry());//第一个条目/键
System.out.println(“GetData B_银行卡”+people.get(“B”);//获取B值
System.out.println(“GetData B_lowerKey”+people.lowerEntry(“B”);//降低B
System.out.println(“GetData B_highrKey”+people.higherEntry(“B”);//获得更高的B
System.out.println(“GetData lastEntry”+people.lastEntry());//last entry/key
System.out.println(“***>”);
}
}         
生成/显示带有关键字的跟踪和导航记录。注意:PUT是按降序进行的,但是TreeMap(默认情况下)是按升序重新排列的(请参见“GetData keySet,…entrySet”示例)

与或不与“ToWhere Wego”包一起使用,因为它是公共的
**GetDS main:调用PutDS并。。。
输入
PutDS{A=全部,C=信用卡}
**返回GetDS main:在调用PutDS之后
{{{{导航有用的关键字等}}}
GetData键集[A、B、C、D]
GetData entrySet[A=全部,B=银行卡,C=信用卡,D=大力水手]
GetData 1sEntry A=全部
GetData B_银行卡银行卡
GetData B_lowerKey A=全部
GetData B_highrKey C=信用卡
GetData lastEntry D=大力水手
**  >

换句话说,您不知道如何在Java中初始化静态变量?尝试搜索主题可能重复的No。我已经读到,我正在尝试做的是删除static。我的变量不应该被定义为静态的。我有一些例子,它曾经在非主类中是静态的。那样的话,你的问题太不清楚了,无法回答。因为按照现在的编写方式,您需要一个静态初始值设定项。我将重新读取static init并重试。
GetData 1stEntry  bob=2
GetData bobValue  2
GetData popValue  3
GetData lowerKey  bob=2
GetData highKey   susan=5

//package twowherewego;

import java.util.TreeMap;

public class PutDS {        // Put DS(Data Storage)

public static TreeMap gettMapCashType(){    System.out.println("PutDS - entered " );
    // Create TreeMap=tMapCashType  and "put" two records to tMapCashType
       TreeMap<String, String> tMapCashType = new TreeMap<String, String>();

                tMapCashType.put("C", "Credit Card");  // out of order puts
                tMapCashType.put("A", "All");

System.out.println("PutDS " + tMapCashType);
                return tMapCashType;
            }
    }
//package twowherewego;
import java.util.TreeMap;

public class GetDS{         // Get DS(Data Storage)

    public static void main(String[] args) {  
        System.out.println("Works with or without 'towherewego' package cause it is public");
        System.out.println("** GetDS main: call PutDS and...");
        // Execute PutDS with method=gettMapCashType() 
       TreeMap<String, String> people = PutDS.gettMapCashType();   //class=PutDS "." method
       System.out.println("** back into GetDS main: after call to PutDS ");
                                            // space or no space ='s no difference
       people.put("D","popeye");            // notice this "put" has no space                                            
       people.put("B", "Bank Card");        // notice this "put" has a space after the ","

        System.out.println("{{{ Navigational helpful keywords, etc }}}" );
        System.out.println("GetData keySet      " +people.keySet());         // key 
        System.out.println("GetData entrySet    " +people.entrySet());       // key and value 
        System.out.println("GetData 1stEntry    " +people.firstEntry());     // 1st entry/key 
        System.out.println("GetData B_BankCard  " +people.get("B"));         // get B value 
        System.out.println("GetData B_lowerKey  " +people.lowerEntry("B"));  // get B lower
        System.out.println("GetData B_highrKey  " +people.higherEntry("B")); // get B higher    
        System.out.println("GetData lastEntry   " +people.lastEntry());      // last entry/key

           System.out.println("**  << End of GetDS >>");
    }
    }         

Works with or without 'towherewego' package cause it is public
** GetDS main: call PutDS and...
PutDS - entered 
PutDS {A=All, C=Credit Card}
** back into GetDS main: after call to PutDS 
{{{ Navigational helpful keywords, etc }}}
GetData keySet      [A, B, C, D]
GetData entrySet    [A=All, B=Bank Card, C=Credit Card, D=popeye]
GetData 1stEntry    A=All
GetData B_BankCard  Bank Card
GetData B_lowerKey  A=All
GetData B_highrKey  C=Credit Card
GetData lastEntry   D=popeye
**  << End of GetDS >>