在Java中使用数组

在Java中使用数组,java,arrays,Java,Arrays,我要将数组复制到数组。可能是这样吗 我想从数组“arrayList data”中获取一些值 该数组中包含一个值 我将inkLevels声明为数组,以便为第三行输入值。我只需要最后一行的值。在GetInkLevel内部。我有一些方法,但我肯定这不是好方法。你有什么建议吗 public class ActivityToSmartWatch extends BaseAdapter { private Activity activity; private ArrayList<String[]

我要将数组复制到数组。可能是这样吗

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

该数组中包含一个值

我将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;
}
}
公共类活动ToSmartWatch扩展BaseAdapter{
私人活动;
私有数组列表数据;
专用静态充气机=空;
私有字符串[]inkLevels=新字符串[5];
公共活动ToSmartWatch(活动a,ArrayList InkLevel){
活动=a;
数据=水平;
充气器=(充气器)活动
.getSystemService(上下文布局\充气机\服务);
}
public int getCount(){
返回data.size();
}
公共对象getItem(int位置){
返回位置;
}
公共长getItemId(int位置){
返回位置;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图vi=转换视图;
if(convertView==null)
vi=充气机
.充气(R.layout.listviewitem_墨水,空);
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]);
}
返回vi;
}
公共字符串[]getInkLevel(字符串[]InkLevel,int位置)
{

对于(int i=0;i方法应如下所示:

public String[] getInkLevel(String[] inkLevels, int position)
{
  for (int i = 0; i < data.get(position).length; i++)
  {
    inkLevels[i] = data.get(position)[i];
  }
  return inkLevels;
}
您也可以只返回适用的数组(如果您愿意在
数据中反映更改,请执行以下操作):


不建议使用数组存储有意义的不同值。最好创建一个类来保存您需要的所有相关值,并使用该类的实例


下一步是基于行/列创建多个实例,并使用数据结构(list/map/set等)保存所有实例。

正如我几次所说,与其处理数组,为什么不将其交换为对象呢

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

      // Getters and Setters.
}

然后复制它们只是一个传递引用的例子。

这里不清楚您要完成什么任务;您能更详细地解释一下
inkLevels
的含义吗?您看过了吗?这看起来您可能想使用
Map
,其中
InkColor
c
enum
y
m
k
InkInfo
是一个包装相关级别信息的自定义类-数组通常是OO设计不佳的标志。我也不明白。请尝试将问题重新表述为更一般的问题。为什么使用
String[]因为OP就是这么做的。我只是专注于如何从一个数组复制到另一个数组(这似乎是OP想要的),而不是重写代码。可能最好避免使用
Color
作为类名,因为有
java.awt.Color
,人们可能会感到困惑。确切地说-OP是在“对象拒绝”中:-)
public String[] getInkLevel(int position)
{
  return data.get(position);
}
public class PrinterColour
{
      // Include your data here.
      private String colorName;
      private int level;

      // Getters and Setters.
}