【发布时间】:2019-05-20 14:55:57
【问题描述】:
之前如果我的问题是经典的,我会道歉。但是因为我刚学安卓的Java编程,所以对命令不是很懂。
我目前正在制作一个代码部分如下的应用程序:
Map<Integer, CompTypes> mapCompTypes = new java.util.HashMap();
class CompTypes
{
int androidId;
AndroidViewComponent component;
String text;
String type;
CompTypes(int androidId, AndroidViewComponent component, String type, String text)
{
this.androidId = androidId;
this.component = component;
this.type = type;
this.text = text;
}
}
public void SendMessage(HVArrangement container, String message, String dateTime, int fontSize, int fontColor, int alignment, int startid)
{
int id = Integer.parseInt(ChatBox.this.GetLastId());
TextBox comp = new TextBox(vertical);
comp.getView().setId(id);
comp.Text(message);
comp.FontSize(fontSize);
comp.TextColor(fontColor);
comp.MultiLine(true);
comp.Enabled(false);
comp.RequestFocus();
mapCompTypes.put(Integer.valueOf(id), new CompTypes(id, comp, compType, comp.Text()));
int id2 = id + 1;
Label dt = new Label(vertical);
dt.getView().setId(id2);
((TextView)dt.getView()).setText(android.text.Html.fromHtml(datetime));
dt.HTMLFormat(true);
dt.Text(datetime);
dt.getView().setOnClickListener(this);
dt.getView().setOnLongClickListener(this);
mapCompTypes.put(Integer.valueOf(id2), new CompTypes(id2, dt, compType, dt.Text()));
}
public String GetLastId()
{
// how to get last id?
//return ids;
}
目前我无法从 id 获取值作为 Hashmap 的最后一个 id。 我在这里阅读了许多问题和答案,“地图没有最后一个条目,这不是他们合同的一部分。” 还有其他方法可以获取最后一个 id 吗?
【问题讨论】:
-
A
HashMap没有“last”键,无论是按插入顺序还是按排序顺序。 “最后一个”到底是什么意思? -
这将帮助您找出您的需求:stackoverflow.com/questions/3527216/…?
-
既然要获取最后一个id,就用"LinkedHashMap" (docs.oracle.com/javase/6/docs/api/java/util/LinkedHashMap.html) 和HashMap一模一样,只不过当你遍历它的时候,它会呈现在插入顺序。在这种情况下,您可以获得最后一个 id。
-
为什么要使用
HashMap而不是ArrayList?