gpt4 book ai didi

java - 在 Java 中使用数组

转载 作者:行者123 更新时间:2023-11-30 04:15:38 25 4
gpt4 key购买 nike

我想将数组复制到数组。这样也可以吗?

我想从数组“arrayList data”中获取一些值

该数组包含一个值

enter image description here

我将 inkLevels 声明为数组,为第三行添加值。我只需要最后一行的值。在 GetInkLevel 内。我有一些方法,但我确信这不是好方法。您有什么建议吗?

public class ActivityToSmartWatch extends BaseAdapter {


private Activity activity;
private ArrayList<String[]> data;
private static LayoutInflater inflater = null;
private String[] inkLevels = new String[5];

public ActivityToSmartWatch(Activity a, ArrayList<String[]> inkLevels) {
activity = a;
data = inkLevels;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}


public int getCount() {
return data.size();
}

public Object getItem(int position) {
return position;
}

public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

View vi = convertView;
if (convertView == null)
vi = inflater
.inflate(R.layout.listviewitem_ink, null);

if (data.get(position)[0].equals("C"))
{
TextView inkLevel = (TextView)vi.findViewById(R.id.lvi_colorLevel);
inkLevel.setText(data.get(position)[2]);
}
else if (data.get(position)[0].equals("Y"))
{
TextView inkLevel = (TextView)vi.findViewById(R.id.lvi_colorLevel);
inkLevel.setText(data.get(position)[2]);
}
else if (data.get(position)[0].equals("M"))
{
TextView inkLevel = (TextView)vi.findViewById(R.id.lvi_colorLevel);
inkLevel.setText(data.get(position)[2]);
}
else if (data.get(position)[0].equals("B"))
{
TextView inkLevel = (TextView)vi.findViewById(R.id.lvi_colorLevel);
inkLevel.setText(data.get(position)[2]);
}


return vi;

}


public String[] getInkLevel (String [] inkLevels, int position)
{
for (int i= 0; i <data.size(); i++)
{
inkLevels[i] = data.get(i);
}

return inkLevels;
}
}

最佳答案

正如我多次说过的,为什么不直接将其替换为对象,而不是处理数组呢?

public class PrinterColour
{
// Include your data here.
private String colorName;
private int level;

// Getters and Setters.
}

然后复制它们只是传递引用的情况。

关于java - 在 Java 中使用数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18482188/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com