Java 按相反顺序排列的ArrayList
我需要从第一个元素参数的第一次出现开始,然后以第二个参数的下一次出现结束,将它们按相反的顺序排列 即:Java 按相反顺序排列的ArrayList,java,arraylist,Java,Arraylist,我需要从第一个元素参数的第一次出现开始,然后以第二个参数的下一次出现结束,将它们按相反的顺序排列 即: 可以使用indexOf和lastIndexOf获取元素的正确位置 但是,如果找不到元素,这些方法将返回-1,请注意这一点 public static void main(String[] args) throws Exception { List<String> food = Arrays.asList("tomato", "cheese", "chips", "fruit
可以使用
indexOf
和lastIndexOf
获取元素的正确位置
但是,如果找不到元素,这些方法将返回-1
,请注意这一点
public static void main(String[] args) throws Exception {
List<String> food = Arrays.asList("tomato", "cheese", "chips", "fruit", "pie", "butter", "tea", "buns");
ArrayList<String> lst = reverseOrder(food, "chips", "buns");
System.out.println(lst);
}
private static ArrayList<String> reverseOrder(List<String> food, String start, String end) throws Exception {
int startIndex = food.indexOf(start);
if (startIndex < 0) {
throw new Exception(start + " not found");
}
int endIndex = food.lastIndexOf(end);
if (endIndex < 0) {
throw new Exception(end + " not found");
}
ArrayList<String> lst = new ArrayList<>();
while (endIndex >= startIndex) {
lst.add(food.get(endIndex--));
}
return lst;
}
publicstaticvoidmain(字符串[]args)引发异常{
列出食物=数组;
ArrayList lst=反向排序(食物、“薯条”、“面包”);
系统输出打印项次(lst);
}
私有静态ArrayList reverseOrder(列表食物、字符串开始、字符串结束)引发异常{
int startIndex=食品指数(start);
如果(起始索引<0){
抛出新异常(start+“未找到”);
}
int ENDIX=食品的最新指数(end);
if(endIndex<0){
抛出新异常(end+“未找到”);
}
ArrayList lst=新的ArrayList();
而(endIndex>=startIndex){
第一次添加(食物获取(endIndex--));
}
返回lst;
}
输出
[面包、茶、黄油、馅饼、水果、薯条]
您可以使用indexOf
和lastIndexOf
获得元素的正确位置
但是,如果找不到元素,这些方法将返回-1
,请注意这一点
public static void main(String[] args) throws Exception {
List<String> food = Arrays.asList("tomato", "cheese", "chips", "fruit", "pie", "butter", "tea", "buns");
ArrayList<String> lst = reverseOrder(food, "chips", "buns");
System.out.println(lst);
}
private static ArrayList<String> reverseOrder(List<String> food, String start, String end) throws Exception {
int startIndex = food.indexOf(start);
if (startIndex < 0) {
throw new Exception(start + " not found");
}
int endIndex = food.lastIndexOf(end);
if (endIndex < 0) {
throw new Exception(end + " not found");
}
ArrayList<String> lst = new ArrayList<>();
while (endIndex >= startIndex) {
lst.add(food.get(endIndex--));
}
return lst;
}
publicstaticvoidmain(字符串[]args)引发异常{
列出食物=数组;
ArrayList lst=反向排序(食物、“薯条”、“面包”);
系统输出打印项次(lst);
}
私有静态ArrayList reverseOrder(列表食物、字符串开始、字符串结束)引发异常{
int startIndex=食品指数(start);
如果(起始索引<0){
抛出新异常(start+“未找到”);
}
int ENDIX=食品的最新指数(end);
if(endIndex<0){
抛出新异常(end+“未找到”);
}
ArrayList lst=新的ArrayList();
而(endIndex>=startIndex){
第一次添加(食物获取(endIndex--));
}
返回lst;
}
输出
[面包、茶、黄油、馅饼、水果、薯条]
您试图解决的问题实际上是两个。首先,您需要确定您关心的范围(索引)。然后在适当位置执行反向操作
你有第一部分。第二部分可以通过反复删除和插入值来完成,但我建议改为交换
ArrayList l;
int begin, end;
//reverse everything from "begin" to "end".
while(begin<end){
Object tmp = l.get(begin);
l.set(begin, l.get(end));
l.set(end, tmp);
begin++;end--;
}
此外,如果您需要不同的列表,并且没有像以前那样修改原始列表,您可以获取这样的子列表
list.subList(fromIndex, toIndex)
有了这一点,您可以轻松地结合上述各项执行任务。您试图解决的问题实际上有两个。首先,您需要确定您关心的范围(索引)。然后在适当位置执行反向操作 你有第一部分。第二部分可以通过反复删除和插入值来完成,但我建议改为交换
ArrayList l;
int begin, end;
//reverse everything from "begin" to "end".
while(begin<end){
Object tmp = l.get(begin);
l.set(begin, l.get(end));
l.set(end, tmp);
begin++;end--;
}
此外,如果您需要不同的列表,并且没有像以前那样修改原始列表,您可以获取这样的子列表
list.subList(fromIndex, toIndex)
有了这一点,您可以轻松地结合上述各项执行任务。这部分代码是错误的:
for(int j = startingPos; j<=endingPos; j--){
String p = a.get(j);
food.add(p);
}
for(int j=startingPos;j)的起始位置为1,结束位置为0。在循环验证中,您有“以j=1开始循环,而j这部分代码是错误的:
for(int j = startingPos; j<=endingPos; j--){
String p = a.get(j);
food.add(p);
}
for(int j=startingPos;j)起始位置为1,结束位置为0。在循环验证中,您有“从j=1开始执行循环,而j问题有两部分。首先,我们需要找到满足条件的子列表。一旦有了子列表,我们就可以使用Collections.reverse(列表)
将其反转
首先,找到两个元素(包括)之间的子列表。主要思想是使用indexOf
识别这两个元素的索引,然后对它们调用sublist
。但我们需要记住,索引的顺序可能与我们想象的不同
private static <T> List<T> inclusiveSublist(List<T> src, T from, T to) {
int start = src.indexOf(from), stop = src.indexOf(to);
if (start != -1 && stop != -1) {
// Successfully located both! But they could be in the "wrong" order
if (start <= stop)
// Element from could appear before to in the list (Plus one to include the second element)
return src.subList(start, 1 + stop);
else // Or the other way around
return src.sublist(stop, 1 + start);
}
// Return empty list if we cannot find both elements
return Collections.emptyList();
}
以及测试:
public static void main(String [] args) {
List<String> src = new ArrayList<>();
src.addAll(Arrays.asList("tomato cheese chips fruit pie butter tea buns".split("\\s")));
System.out.println(src);
System.out.println(inclusiveReverseSublist(src, "fruit", "buns"));
System.out.println(inclusiveReverseSublist(src, "cheese", "tomato"));
}
publicstaticvoidmain(字符串[]args){
List src=new ArrayList();
src.addAll(Arrays.asList(“番茄奶酪片水果派黄油茶包”);
系统输出打印LN(src);
System.out.println(包括VersePublist(src,“水果”、“面包”));
System.out.println(包括VersePublist(src,“奶酪”、“番茄”);
}
关于子列表()
从Java API文档中,子列表
返回指定的fromIndex(包含)和toIndex(独占)之间此列表部分的视图。(如果fromIndex和toIndex相等,则返回的列表为空。)此列表支持返回的列表,因此返回列表中的非结构更改将反映在此列表中,反之亦然。返回的列表支持此列表支持的所有可选列表操作
因此,上面的代码实际上会修改原始列表,将System.out.println(src);
移到底部将证实这一点。问题有两个部分。首先,我们需要找到满足条件的子列表。一旦有了子列表,我们就可以使用Collections.reverse(list list)
将其反转
首先,找到两个元素(包括)之间的子列表。主要思想是使用indexOf
识别这两个元素的索引,然后对它们调用sublist
。但我们需要记住,索引的顺序可能与我们想象的不同
private static <T> List<T> inclusiveSublist(List<T> src, T from, T to) {
int start = src.indexOf(from), stop = src.indexOf(to);
if (start != -1 && stop != -1) {
// Successfully located both! But they could be in the "wrong" order
if (start <= stop)
// Element from could appear before to in the list (Plus one to include the second element)
return src.subList(start, 1 + stop);
else // Or the other way around
return src.sublist(stop, 1 + start);
}
// Return empty list if we cannot find both elements
return Collections.emptyList();
}
以及测试:
public static void main(String [] args) {
List<String> src = new ArrayList<>();
src.addAll(Arrays.asList("tomato cheese chips fruit pie butter tea buns".split("\\s")));
System.out.println(src);
System.out.println(inclusiveReverseSublist(src, "fruit", "buns"));
System.out.println(inclusiveReverseSublist(src, "cheese", "tomato"));
}
publicstaticvoidmain(字符串[]args){
List src=new ArrayList();
src.addAll(Arrays.asList(“番茄奶酪片水果派黄油茶包”);
系统输出打印LN(src);
System.out.println(包括VersePublist(src,“水果”、“面包”));
系统输出打印项次(包括
public static void main(String [] args) {
List<String> src = new ArrayList<>();
src.addAll(Arrays.asList("tomato cheese chips fruit pie butter tea buns".split("\\s")));
System.out.println(src);
System.out.println(inclusiveReverseSublist(src, "fruit", "buns"));
System.out.println(inclusiveReverseSublist(src, "cheese", "tomato"));
}