Java 引用字符串的最后一个字母';在arraylist中

Java 引用字符串的最后一个字母';在arraylist中,java,Java,我必须将一些字符串存储到ArrayList中,但我不太熟悉如何引用ArrayList中的值索引。我知道我可以使用类似于: String value = mylist.get(1); 要获取,请参阅列表中的第二项。我想做的是参考项目的最后一个字母 这是我的密码: ArrayList<String> alist = new ArrayList<String>(); alist.add("bobb"); alist.add("Hamm"); alist.add("odffe"

我必须将一些字符串存储到
ArrayList
中,但我不太熟悉如何引用
ArrayList
中的值索引。我知道我可以使用类似于:

String value = mylist.get(1);
要获取,请参阅列表中的第二项。我想做的是参考项目的最后一个字母

这是我的密码:

ArrayList<String> alist = new ArrayList<String>();
alist.add("bobb");
alist.add("Hamm");
alist.add("odffe");
ArrayList alist=new ArrayList();
列表。添加(“bobb”);
列表。添加(“Hamm”);
添加(“odffe”);
现在,我试图从
“odffe”
中获取字母
“e”
的索引。这在ArrayList中是可能的吗?我尝试过做一些事情,但我认为我把它当作一个普通数组,而不是
ArrayList


我不能使用
charAt()
,因为它说它需要char,但我有Java.lang.String,我不知道如何处理这个问题。

您应该将此任务分为两部分:

  • 查找列表中的最后一个字符串
  • 找到该字符串的最后一个字母
这些任务中的每一项都非常简单——我意识到有两项任务让你感到困惑。我会这样写:

// Note: this will throw an exception if the list is empty or null
String lastElement = alist.get(alist.size() - 1);

// Note: this will throw an exception if the string is empty or null
char lastCharacter = lastElement.charAt(lastElement.length() - 1);

列表中每个字符串的最后一个字母应与字符串的长度一致,因此您可以尝试:

List<Integer> indices = mylist.stream()
    .map(x -> x == null ? -1 : x.length() - 1)
    .collect(Collectors.toList());
List index=mylist.stream()
.map(x->x==null?-1:x.length()-1)
.collect(Collectors.toList());

发布后,我发现了第一步,但第二步我不知道我能做什么。谢谢我想你想要字符串的最后一个字符。。。但是您想为一个特定项目执行此操作吗?还是要列出最后的所有字符?