[英]Update Element in ArrayList
我在更新arraylist中的元素時遇到問題。 我有ViewLabelINScreen
類,並從該類構建元素的ViewLabelINScreen
,但是當我更新此數組列表中的元素時,同一對象中沒有另一個元素時,將獲得默認值。 我如何只更改此元素而又不更改其他元素。
我在其他類中更改了該元素,並添加了該元素,並且需要此數組列表為靜態。
ViewLabelINScreen:
public class ViewLabelINScreen {
public int xP;
public int yP;
public String nameLabel;
public int stringWidth4;
public int fontHeight;
public boolean stored;
public ViewLabelINScreen(int XP,int YP,String NameLabel,int StringWidth4 ,int FontHeight )
{
xP=XP; yP=YP; nameLabel=NameLabel;stringWidth4=StringWidth4;fontHeight=FontHeight;
}
//this contractor for change namelabel
public ViewLabelINScreen (String Namelabel)
{
nameLabel=Namelabel;
}
public ViewLabelINScreen () {
}
}
下列類將元素添加到arraylist中。
public class WgsGrid {
//Added by A.B
MapSelection mapSelection;
public static ArrayList<ViewLabelINScreen> INFOLable = new ArrayList<ViewLabelINScreen>();
for (int i4 = hMin; i4 <= hMax; i4++)
{
INFOLable.add(new ViewLabelINScreen(labelRectX, labelRectY - yOffset4, SetLabels.getYlabels()[counter2],stringWidth4,fontHeight));
}
}
此類NeedChangeArrayList更改元素:
class NeedChangeArrayList {
public void ChangeAraay()
{
if (WgsGrid.INFOLable.isEmpty() != true)
{
for (int s = 0; s < ylabels.length; s++)
{
WgsGrid.INFOLable.set(s, new ViewLabelINScreen(ylabels[s]));
}
//ylabels[s] string array get value from array and put in nameLabel
}
}
}
您是否要更改數組列表中的所有nameLabel值? 如果是這樣,您可以在for循環中嘗試以下操作:
for(int s =0; s< WgsGrid.INFOLable.size();s++){
WgsGrid.INFOLable.get(s).setNameLabel("whatever");
}
但是,您需要為nameLabel創建一個setter方法。
public void setNameLabel(String nameLabel){
this.nameLabel = nameLabel;
}
(我已經從ylabels.length更改為arraylist的大小。不確定您對ylabels所做的工作)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.