Java 删除数组中最后一次出现的元素
如果数组的一行包含以下字符:Java 删除数组中最后一次出现的元素,java,arrays,Java,Arrays,如果数组的一行包含以下字符: line = "I appreciate you helping me out!" 假设我想删除字母“e”的最后一次出现。 怎么用? i、 e.结果应为: "I appreciate you helping m out!" 这是我的想法,我知道语法是错误的。我从26开始,因为这是字符串长度上最后一次出现位置“e” for (int i = 26; i < line.length() ; i++) line.chars('e') = (line.ch
line = "I appreciate you helping me out!"
假设我想删除字母“e”的最后一次出现。
怎么用?
i、 e.结果应为:
"I appreciate you helping m out!"
这是我的想法,我知道语法是错误的。我从26开始,因为这是字符串长度上最后一次出现位置“e”
for (int i = 26; i < line.length() ; i++)
line.chars('e') = (line.chars('') && line.chars(i));
}
for(int i=26;i
或
或
也可以使用如下所示的正则表达式:
String word = "I appreciate you helping me out!";
System.out.println(word.replaceAll("[e]([^e]*)$","$1"));
输出:
I appreciate you helping m out!
也可以使用如下所示的正则表达式:
String word = "I appreciate you helping me out!";
System.out.println(word.replaceAll("[e]([^e]*)$","$1"));
输出:
I appreciate you helping m out!
请尝试下面的代码片段。理想情况下,它应该以通用的方式工作
StringBuilder line = new StringBuilder(
"I appreciate you helping me out!");
System.out.println("Given input String :" + line);
int lastOccuranceIndex = 0;
int deleteIndex = 0;
char[] charr = line.toString().toLowerCase().toCharArray();
Map<Character, List<Integer>> charMap = new LinkedHashMap<Character, List<Integer>>();
List<Integer> indexList = null;
for (int i = 0; i < charr.length; i++) {
if (charMap.containsKey(charr[i])) {
indexList = charMap.get(charr[i]);
indexList.add(i);
charMap.put(charr[i], indexList);
} else if (Character.isAlphabetic(charr[i])) {
indexList = new ArrayList<Integer>();
indexList.add(i);
charMap.put(charr[i], indexList);
}
}
for (Entry<Character, List<Integer>> entry : charMap.entrySet()) {
indexList = entry.getValue();
if (indexList.size() > 2) {
// System.out.println(entry.getKey()
// +" last but one : "+indexList.get(indexList.size() -2));
if (indexList.get(indexList.size() - 2) > lastOccuranceIndex) {
lastOccuranceIndex = indexList.get(indexList.size() - 2);
deleteIndex = indexList.get(indexList.size() - 1);
}
}
}
System.out.println("last occurance character index "
+ lastOccuranceIndex + " and the character to delete is :"
+ charr[lastOccuranceIndex]);
char deleteChar = line.charAt(deleteIndex);
System.out.println("deleteChar :" + deleteChar + " at index :"
+ deleteIndex);
line = line.deleteCharAt(deleteIndex);
System.out.println("String content after delete operation : " + line);
StringBuilder行=新建StringBuilder(
“我感谢你帮了我的忙!”;
System.out.println(“给定输入字符串:“+行”);
int lastOccuranceIndex=0;
int deleteIndex=0;
char[]charr=line.toString().toLowerCase().toCharArray();
Map charMap=新建LinkedHashMap();
列表索引列表=null;
for(int i=0;i2){
//System.out.println(entry.getKey()
//+“最后一个:”+indexList.get(indexList.size()-2));
if(indexList.get(indexList.size()-2)>lastOccuranceIndex){
lastOccuranceIndex=indexList.get(indexList.size()-2);
deleteIndex=indexList.get(indexList.size()-1);
}
}
}
System.out.println(“上次出现字符索引”
+lastOccuranceIndex+“要删除的字符为:”
+charr[lastOccuranceIndex]);
char deleteChar=line.charAt(deleteIndex);
System.out.println(“deleteChar:+deleteChar+”位于索引处:”
+删除索引);
line=line.deleteCharAt(deleteIndex);
System.out.println(“删除操作后的字符串内容:“+行”);
输出:
给定输入字符串:感谢您的帮助
最后出现的字符索引18和要删除的字符是:e
deleteChar:e在索引:26处
删除操作后的字符串内容:感谢您的帮助 请尝试下面的代码片段。理想情况下,它应该以通用的方式工作
StringBuilder line = new StringBuilder(
"I appreciate you helping me out!");
System.out.println("Given input String :" + line);
int lastOccuranceIndex = 0;
int deleteIndex = 0;
char[] charr = line.toString().toLowerCase().toCharArray();
Map<Character, List<Integer>> charMap = new LinkedHashMap<Character, List<Integer>>();
List<Integer> indexList = null;
for (int i = 0; i < charr.length; i++) {
if (charMap.containsKey(charr[i])) {
indexList = charMap.get(charr[i]);
indexList.add(i);
charMap.put(charr[i], indexList);
} else if (Character.isAlphabetic(charr[i])) {
indexList = new ArrayList<Integer>();
indexList.add(i);
charMap.put(charr[i], indexList);
}
}
for (Entry<Character, List<Integer>> entry : charMap.entrySet()) {
indexList = entry.getValue();
if (indexList.size() > 2) {
// System.out.println(entry.getKey()
// +" last but one : "+indexList.get(indexList.size() -2));
if (indexList.get(indexList.size() - 2) > lastOccuranceIndex) {
lastOccuranceIndex = indexList.get(indexList.size() - 2);
deleteIndex = indexList.get(indexList.size() - 1);
}
}
}
System.out.println("last occurance character index "
+ lastOccuranceIndex + " and the character to delete is :"
+ charr[lastOccuranceIndex]);
char deleteChar = line.charAt(deleteIndex);
System.out.println("deleteChar :" + deleteChar + " at index :"
+ deleteIndex);
line = line.deleteCharAt(deleteIndex);
System.out.println("String content after delete operation : " + line);
StringBuilder行=新建StringBuilder(
“我感谢你帮了我的忙!”;
System.out.println(“给定输入字符串:“+行”);
int lastOccuranceIndex=0;
int deleteIndex=0;
char[]charr=line.toString().toLowerCase().toCharArray();
Map charMap=新建LinkedHashMap();
列表索引列表=null;
for(int i=0;i2){
//System.out.println(entry.getKey()
//+“最后一个:”+indexList.get(indexList.size()-2));
if(indexList.get(indexList.size()-2)>lastOccuranceIndex){
lastOccuranceIndex=indexList.get(indexList.size()-2);
deleteIndex=indexList.get(indexList.size()-1);
}
}
}
System.out.println(“上次出现字符索引”
+lastOccuranceIndex+“要删除的字符为:”
+charr[lastOccuranceIndex]);
char deleteChar=line.charAt(deleteIndex);
System.out.println(“deleteChar:+deleteChar+”位于索引处:”
+删除索引);
line=line.deleteCharAt(deleteIndex);
System.out.println(“删除操作后的字符串内容:“+行”);
输出:
给定输入字符串:感谢您的帮助
最后出现的字符索引18和要删除的字符是:e
deleteChar:e在索引:26处
删除操作后的字符串内容:感谢您的帮助 Regex拯救:
line = line.replaceAll("e(?=[^e]*$)", "");
正则表达式(?=[^e]*$)
是一种前瞻性的方法,它要求在匹配e
之后的任何地方都不出现e
。正则表达式:
line = line.replaceAll("e(?=[^e]*$)", "");
正则表达式
(?=[^e]*$)
是一种前瞻性的方法,它要求在匹配e
之后的任何地方都不出现e
。您可以向后重建字符串并删除第一次出现的字符。运行“idea”时会发生什么?请尝试行。replaceFirst((*)e([^e]*),“$1$2”)
。您可以向后重建字符串并删除第一次出现的字符。运行“idea”时会发生什么情况?请尝试行。replaceFirst((*)e([^e]*),“$1$2”)
。