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 Answers
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]
1 Comment
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
if statement to remove element from list if it consists of one character.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
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
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]
ArrayListofString?[10, +, 20, -, 129]-->[10, +, 20, -, 12]-->[10, +, 20, -, 1]--> ???