[英]return array from method to get objects from a hashmap - java
I have a hashmap containing objects created via a constructor. 我有一个包含通过构造函数创建的对象的哈希图。 These objects are in the hashmap
这些对象在哈希图中
I have a function that compares two arrays. 我有一个比较两个数组的函数。 The one array has been manually created, but the second array is created via a method.
一个数组是手动创建的,但是第二个数组是通过方法创建的。 I need only the values from the hashmap.
我只需要哈希图中的值。
String[] checkOne= { "1.2:3,4:Brown", "1.1:5,4:Green" };
Map<String,RoundBrick> hashMap = new HashMap<String, RoundBrick>();
public void addBrick(RoundBrick roundBrick){
hashMap.put(roundBrick.getRef, roundBrick)
}
checkBrick(getBricks(), one){
...
}
public static boolean checkBrick(Brick[] brick, String[] checkOne){
...
}
Where I am stuck is with this method... 我陷入困境的是这种方法...
public Brick[] getBricks(){
How do I convert the objects from the hashmap to an array, so I can compare the contents of the two arrays? 如何将对象从哈希图转换为数组,以便可以比较两个数组的内容? I can go...
我可以去...
public Brick[] getBricks() {
Brick[] bricks = {hashmap.value().toArray()};
return bricks;
}
But this give error saying cannot convert from Object[] to Brick 但这给了错误,说不能从Object []转换为Brick
If I change the method to the following... 如果我将方法更改为以下内容...
public Brick[] getBricks() {
String a = Arrays.toString(hashmap.values().toArray());
return a;
}
The error I get on "a" is "cannot convert from String to Brick[]" 我在“ a”上遇到的错误是“无法从String转换为Brick []”
And when adding the line below, it prints out the array. 并且在下面添加行时,它会打印出数组。
System.out.print(hashmap.values());
[1.2:3,4:Brown, 1.1:5,4:Green]
can anyone point me into the right direction? 谁能指出我正确的方向?
public Brick[] getBricks() {
return hashmap.values().toArray(new Brick[0]);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.