0

Please I need help with this, if I have an arraylist with elements [10,+,20,-,129]. I want to know how I can delete from behind the elements in the last index in the arraylist e.g removing 9 so that the array becomes [10,+,20,-,12]. Thanks

6
  • do you want to remove third digit in any item? Commented May 24, 2016 at 7:02
  • Is it an ArrayList of String? Commented May 24, 2016 at 7:03
  • Yes it is an arraylist of string Commented May 24, 2016 at 7:09
  • So if you repeat the operation, what is the expected behavior? [10, +, 20, -, 129] --> [10, +, 20, -, 12] --> [10, +, 20, -, 1] --> ??? Commented May 24, 2016 at 7:10
  • 1
    If you figured out how to build that list in the first place, what exactly is the problem you are facing with doing this? Show us what you have tried. Commented May 24, 2016 at 7:12

6 Answers 6

1

As its list of string elements, first take the last element and check whether the length of last element. If length is 1 then remove it, else trim one char from last.

// Adding element in list
list.add("1")
list.add("-");
list.add("23");
list.add("+");
list.add("123");
list.add("-");
// Size of List 
int size = list.size();
// Getting Last element
String  lastValue = list.get(size-1);
// check the length of last element
if(lastValue.length()<=1)
     list.remove(size - 1);
else
     list.set(size-1,lastValue.substring(0,lastValue.length()-1)); 

Hope it will solve the issue

Input : [1, -, 23, +, 123, -]
Output : [1, -, 23, +, 123]
Sign up to request clarification or add additional context in comments.

1 Comment

While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. - From review
0

I hope this may suites your needs:

String last = list.remove(list.size() - 1);
list.insert(last.substring(0,last.length() -1);

Disclaimer: As I saw the diversity of the elements in your example I assumed the list is a list of Strings.

Comments

0
List elements = Arrays.asList("10","+","20","-","144429");
String str = (String) elements.get(elements.size()-1);  //get value of last element
str = str.substring(0, str.length()-1); //get all characters besides last one
elements.set(elements.size()-1, str);   //replace last element in list

2 Comments

Code works fine for normal numbers, but not for operators like(+,-,)
For single character elements you receives empty String element of list. You can add if statement to remove element from list if it consists of one character.
0

Try get the item on the list, convert that item to string if it is not an array list of strings remove the digit you what by split or substring method then convert back to integer and add to the ArrayList. example send your Array list to the generate new List

public ArrayList<String> globalStrings;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    globalStrings = new ArrayList<>();
    globalStrings.add("10");
    globalStrings.add("+");
    globalStrings.add("-");
    globalStrings.add("129");

    //Before
    checklist(globalStrings);
    globalStrings = getNewStrings(globalStrings);
    //After
    checklist(globalStrings);


}

getNewStrings returns a new ArrayList string with the last index item deleted by one

 private ArrayList<String> getNewStrings(ArrayList<String> generateNew) {

    String getString = generateNew.get(generateNew.size() - 1);

    getString = getString.substring(0,(getString.length() - 1));

    generateNew.remove(generateNew.size() - 1);
    generateNew.add(getString);

    return generateNew;
}

To check the list, you can use the method below

private void checklist(ArrayList<String > check) {
    for(int count =0; count <check.size(); count ++) {
        Log.e("TAG", "List " + check.get(count));
    }
}

2 Comments

If the operation is repeated it should continue removing the last digit of the last item in the arraylist till the arraylist becomes empty
I just edited my post with codes example, try that to get your items deleted by one int.
0

Try Like this it may be work for you

ArrayList<String> arrayList = new ArrayList<>();
//add your data to this list
//suppose
// arrayList.add("10");
// arrayList.add("+");
// arrayList.add("20");
// arrayList.add("-");
// arrayList.add("129");
btnClick = (Button)findViewById(R.id.btnClick);

 btnClick.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(arrayList.size() > 0) {
                String lastOne = arrayList.get(arrayList.size() - 1);
                String newOne = removeLastChar(lastOne);
                arrayList.remove(arrayList.size() - 1);
                if (!newOne.equalsIgnoreCase("")) {
                    arrayList.add(arrayList.size(), newOne);
                }
                for (String item : arrayList) {
                    Log.d("TAG", item);

                }

            }
        }
    });
}
public String removeLastChar(String s) {
    if (s == null || s.length() == 0) {
        return s;
    }
    return s.substring(0, s.length()-1);
}

Comments

0

This is a simple program to solve your problem. You can add the null check conditions to make it better.

        List<String> a= new ArrayList<>();
    a.add("123");
    a.add("856");
    a.add("85");
    a.add("785");

    String trim = a.get(a.size()-1);
    trim = trim.substring(0,trim.length()-1);
   if(trim.equals("")){
       a.remove(a.size()-1);
   }
   else{
       a.set(a.size()-1,trim);
   }
    System.out.println(a);

output: [123, 856, 85, 78]

3 Comments

Code works fine for normal numbers, but not for operators like(+,-,)
it will , you should have a arraylist of type string.
edited the answer to remove if it is blank entry after removing single chars like +,-

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.