1

i have a arraylist called arrstatus which has a set of Numeric values stored dynamically. I have to Replace those Numeric Values with Unique String values. I tried various methods it doesnt seem to be working i tries Set method, didnt work,

// int[] flags = new int[arrstatus_old.size()];

int [] c =  new int [arrtaskid.size()];
Toast.makeText(MyTask.this, "C:"+c, Toast.LENGTH_LONG).show(); 

     for(int j=1;j<= c.length;j++)
     {

        if(arrstatus.get(j).equals("1"))
         {
             arrstatus_old.set(j, "Saved");
         }
        else if(arrstatus.get(j).equals("2"))
        {
            arrstatus_old.set(j, "Assigned");
        }
        else if(arrstatus.get(j).equals("3"))
        {
            arrstatus_old.set(j, "Accepted");
        }
        else if(arrstatus.get(j).equals("4"))
        {
            arrstatus_old.set(j, "Rejected");
        }

     }

I am not getting the size of the arrastutus, it says 0 and hence no values are getting replaced, any better idea to replace the value.?

9
  • j<= c.length This might cause ArrayIndexOutOfBoundsException. Commented Jan 7, 2014 at 7:08
  • @ᴍarounᴍaroun - Not might.. it will (unless he gets some other exception before that..) :P Commented Jan 7, 2014 at 7:09
  • Ya just change it to j <= c.length - 1 Commented Jan 7, 2014 at 7:10
  • @TheLostMind Depends on the ifs on the last index.. Commented Jan 7, 2014 at 7:10
  • @Sanj FYI array index starts from 0 Commented Jan 7, 2014 at 7:10

2 Answers 2

1

Your ArrayList in which 1 i.e. numbers are stored should store numbers in String format.

Your arrayList should be like ArrayList<String>

        ArrayList<String> arrstatus = new ArrayList<String>();
        arrstatus.add("1");
        arrstatus.add("2");
        arrstatus.add("3");

As you are checking in arraylist using equals you'll have add numbers as a String.

if (arrstatus.get(i).equals("1")) {
    arrstatus.set(i, "Saved");
}if (arrstatus.get(i).equals("2")) {
    arrstatus.set(i, "Assigned");
}if (arrstatus.get(i).equals("3")) {
    arrstatus.set(i, "Accepted");
}

and so on...

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

4 Comments

i have ArrayList<String> arrstatus = new ArrayList<String>(); and the values are dynamically stored from server.
@sanj Problem is with your arraylist to which you want to change. In order to change the element, it must contain the element that you are checking (In condition).
@JavaLearner ArrayList.add(index, element) will shift the existing element the "index" position to right.. Refer docs.oracle.com/javase/6/docs/api/java/util/… he wants to replace the existing element
@Jayaprasad I have edited my answer. It was written by mistake. Will u please remove your downvote?
0
ArrayList<String> arrstatus = new ArrayList<String>();
        arrstatus.add("1");
        arrstatus.add("2");
        arrstatus.add("3");
arrstatus.add("4");

for(int j=0;j< c.length;j++)
     {

        if(arrstatus.get(j).equals("1"))
         {
             arrstatus_old.set(j, "Saved");
         }
        else if(arrstatus.get(j).equals("2"))
        {
            arrstatus_old.set(j, "Assigned");
        }
        else if(arrstatus.get(j).equals("3"))
        {
            arrstatus_old.set(j, "Accepted");
        }
        else if(arrstatus.get(j).equals("4"))
        {
            arrstatus_old.set(j, "Rejected");
        }

     }

Comments

Your Answer

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