1

I want to copy array to array. Is it possible like this?

I want to take some values from an array "arrayList data"

In that array contained an values

enter image description here

I declare inkLevels as my array to put value for the third row. I only need the value from the last row. Inside GetInkLevel. I have place some method, but I sure it is not the good way. Do you have any recomendations?

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;
}
}
3
  • 1
    It's not clear what task you're trying to accomplish here; can you explain more what inkLevels means in more detail? Have you looked at Arrays#copyOf? Commented Aug 28, 2013 at 7:56
  • This looks like you may want to use a Map<InkColor,InkInfo> where InkColor is a enum of c,y,m,k and InkInfo is a custom class that wraps the relevant level info - arrays of arrays are often a sign of poor OO design. Commented Aug 28, 2013 at 7:58
  • I dont't get it either please try to rephrase the question to be more general. Commented Aug 28, 2013 at 7:58

3 Answers 3

2

As I've said a few times, instead of handling arrays, why not just swap it out for objects?

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

      // Getters and Setters.
}

Then copying them is simply a case of passing the references across.

Sign up to request clarification or add additional context in comments.

2 Comments

Probably best to avoid using Colour as the class name as there is a java.awt.Color and people may get confused.
Exactly - OP is in "Object Denial" :-)
0

The method should look like this:

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;
}

You were trying to assign elements in the array from elements in the ArrayList, instead of elements from some array in the ArrayList.

System.arrayCopy simplifies it:

public String[] getInkLevel(String[] inkLevels, int position)
{
  System.arraycopy(data.get(position), 0, inkLevels, 0, data.get(position).length);
  return inkLevels;
}

You could also just return the applicable array (if you're happy having changes reflected in data):

public String[] getInkLevel(int position)
{
  return data.get(position);
}

2 Comments

Why use String[] at all?
'cause that's what OP did. I'm just focussing on how to copy from one array to another (which seems to be what OP wants), not rewriting the code.
0

Using arrays to store meaningful and different values is not recommended. It is better to create a class to hold all relevant values that you need and use instances of that class.

Next step is to create several instances based on row/col and use a data structure (list/map/set etc.) to hold all instances.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.