[英]Java Treemap and ArrayList transfer item
I have the following code, a Treemap with a arraylist of fruits. 我有以下代码,一个带有水果arraylist的Treemap。 In the removeAndAdd function, I would like to remove [apple,orange] and add it into bList of the container 2. but the display came out with additional bracket []. 在removeAndAdd函数中,我想删除[apple,orange]并将其添加到容器2的bList中。但是显示出来了另外的括号[]。 Is my method correct? 我的方法是否正确?
public class TreeMapEx {
private TreeMap<Integer, List<String>> tMap = new TreeMap<Integer, List<String>>();
private List<String> aList = new ArrayList<String>();
private List<String> bList = new ArrayList<String>();
public static void main(String[] args) {
TreeMapEx tm = new TreeMapEx();
tm.addToTree();
tm.addToList(1);
tm.showItem(1);
tm.showItem(2);
tm.removeAndAdd(1);
tm.showItem(2);
}
private void addToTree() {
tMap.put(1, aList);
bList.add("dragonfruit");
tMap.put(2, bList);
}
private void addToList(int item) {
if (tMap.containsKey(item)) {
aList = new ArrayList<String>();
aList.add("apple");
aList.add("orange");
tMap.put(item, aList);
System.out.println(item + " added");
} else {
System.out.println(item + " not found");
}
}
private void showItem(int item){
System.out.println(item+" contain " + tMap.get(item));
}
private void removeAndAdd(int item){
if (tMap.containsKey(item) && tMap.containsValue(aList)) {
//remove everything from 1 and add to 2
aList = new ArrayList<String>();
List<String> temp;
temp = tMap.get(item);
bList.add(temp.toString());
}
}
}
Output:
1 added
1 contain [apple, orange]
2 contain [dragonfruit]
2 contain [dragonfruit, [apple, orange]]
How to remove the additional bracket of [apple, orange] in container 2. 如何删除容器2中[苹果,橙色]的附加支架。
To something like this 对于这样的事情
1 added
1 contain [apple, orange]
2 contain [dragonfruit]
2 contain [dragonfruit,apple, orange]
Additional bracket of [apple, orange] is printing becaus the following code is used in method showItem . [apple,orange]的附加括号是打印,因为方法showItem中使用了以下代码。
System.out.println(item+" contain " + tMap.get(item)); System.out.println(item +“contains”+ tMap.get(item));
It is equal to print the elements of List using the following code. 它等于使用以下代码打印List的元素。
System.out.println(item+" contain " + tMap.get(item).toString() ); System.out.println(item +“ contains ”+ tMap.get(item).toString() );
Note: tMap.get(item) is a List type (List) 注意:tMap.get(item)是List类型(List)
If you want to display the elements without additional bracket. 如果要显示没有附加括号的元素。 You can use the following code: 您可以使用以下代码:
private void showItem(int item){
System.out.println(item+" contain ");
for(String str:tMap.get(item) )
{
System.out.println( str);
}
}
In addition, one more thing is not using correctly. 此外,还有一件事没有正确使用。 Please refer to method removeAndAdd . 请参考方法removeAndAdd 。
Change bList.add(temp.toString()) ; 更改bList.add(temp.toString()) ; to bList.addAll(temp) ; 到bList.addAll(temp) ;
If you use bList.add(temp.toString()), List elements with [apple, orange] will be added to bList as a string. 如果使用bList.add(temp.toString()),带有[apple,orange]的List元素将作为字符串添加到bList。 Actually, you would like to add it with apple and orange seperately. 实际上,你想单独添加苹果和橙色。 Use bList.addAll(temp) ; 使用bList.addAll(temp) ;
To see why Collection, such as List print additional bracket is added . 要查看为什么要添加List,例如List打印附加括号 。 Please refer to the source code of java.util.AbstractCollection , which overrides toString method. 请参阅java.util.AbstractCollection的源代码, 该代码覆盖toString方法。 Code is as follows: 代码如下:
public String toString() {
Iterator<E> it = iterator();
if (! it.hasNext())
return "[]";
StringBuilder sb = new StringBuilder();
sb.append('[');
for (;;) {
E e = it.next();
sb.append(e == this ? "(this Collection)" : e);
if (! it.hasNext())
return sb.append(']').toString();
sb.append(',').append(' ');
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.