简体   繁体   English

java子列表删除

[英]java sublist removal

I'm trying to create a list of objects and then a sublist and then delete all the elements in the sublist and then once again display the main list. 我正在尝试创建一个对象列表,然后创建一个子列表,然后删除子列表中的所有元素,然后再次显示主列表。 However when I try to remove elements from a sublist I get error at runtime indexoutofbounds and unknown source. 但是,当我尝试从子列表中删除元素时,我在运行时indexoutofbounds和未知来源时收到错误。 How to fix that so that the app works? 如何解决这个问题,以便应用程序有效?

import java.util.*;

class Eval{
    Eval(){         
    }   
}

public class Ch11Ex7 {
    public static void main(String[] args){ 

        Eval e1 = new Eval();
        Eval e2 = new Eval();
        Eval e3 = new Eval();
        Eval e4 = new Eval();
        Eval e5 = new Eval();

        Eval[] eva = {e1, e2, e3, e4, e5};

        //ArrayList<Eval> ev = new ArrayList<Eval>(Arrays.asList(eva));
        List ev = Arrays.asList(eva);
        List<Eval> sub = ev.subList(1, 3);  
        for(int i=0; i< ev.size() ; i++)
              System.out.println(ev.get(i));        
        System.out.println("Sublist");  
        for(int i=0; i< sub.size() ; i++)
              System.out.println(sub.get(i));   
        System.out.println("Remove element");
        sub.remove(2);
    }
}

The second index of subList is exclusive, so if you want the elements between [1..3] you need to use: subList的第二个索引是独占的,所以如果你想要[1..3]之间的元素你需要使用:

List<Eval> sub = ev.subList(1, 4); 

Furthermore, what you are trying to do will not work anyway, because the List implementation returned by subList does not implement the remove operation, so you will get a java.lang.UnsupportedOperationException . 此外,您尝试执行的操作无论如何都不会起作用,因为subList返回的List实现没有实现remove操作,因此您将获得java.lang.UnsupportedOperationException

You should create sub as an ArrayList instead: 您应该将sub创建为ArrayList

ArrayList<Eval> sub = new ArrayList<Eval>(ev.subList(1, 4));

Java Lists are zero indexed, so: Java列表为零索引,因此:

List<Eval> sub = ev.subList(1, 3) // = {e2, e3} (subList is not inclusive on second index)

And

sub.remove(2); // Attempts to remove 3rd element from 2 element list

So, reduce your indexes by one. 因此,将索引减少一个。

import java.util.*;

class Eval{
Eval(){         
}   
}

public class Ch11Ex7 {
public static void main(String[] args){ 

    Eval e1 = new Eval();
    Eval e2 = new Eval();
    Eval e3 = new Eval();
    Eval e4 = new Eval();
    Eval e5 = new Eval();

    Eval[] eva = {e1, e2, e3, e4, e5};

    //ArrayList<Eval> ev = new ArrayList<Eval>(Arrays.asList(eva));
    List ev = Arrays.asList(eva);
    List<Eval> sub = ev.subList(0, 2); 
    sub = new ArrayList<Eval>(sub);
    for(int i=0; i< ev.size() ; i++)
          System.out.println(ev.get(i));        
    System.out.println("Sublist");  
    for(int i=0; i< sub.size() ; i++)
          System.out.println(sub.get(i));   
    System.out.println("Remove element");
    sub.remove(1);
}

} }

There is no item with index 2 on your sublist sub So the error is self explanatory. 子列表sub上没有索引2的项目因此错误是自解释的。

The indexing begins at 0. 索引从0开始。

In your case 在你的情况下

sub =  = {e2, e3}
sub[0] = e2
sub[1] = e3

So, when you try to remove item at index 2, you get the runtime exception. 因此,当您尝试在索引2处删除项时,您将获得运行时异常。 If you are aim is to remove second element from the list, you should call 如果你的目标是从列表中删除第二个元素,你应该打电话

sub.remove(1)

You want to say sub.remove(1) AND you want to declare the list as an ArrayList - not using Arrays.asList . 你想说sub.remove(1)并且你想要将列表声明为ArrayList - 而不是使用Arrays.asList Your code as is will throw an UnsupportedOperationException because Arrays list is fixed length. 您的代码将抛出UnsupportedOperationException因为Arrays列表是固定长度的。

As @Peter said, indexes in Java are 0-based, so you need to use: 正如@Peter所说,Java中的索引是基于0的,所以你需要使用:

sub.remove(1)

Still , with your current program you will get an UnsupportedOperationException, as Arrays.asList() returns a fixed-size list that doesn't support remove (which will be reflected to ev because subList() reflects changes to the original list). 仍然 ,使用当前程序,您将获得UnsupportedOperationException,因为Arrays.asList()返回不支持remove的固定大小列表(由于subList()反映了对原始列表的更改,因此将反映到ev)。

You need to use an ArrayList or similar resizable implementation of the List interface. 您需要使用ListList或类似的可调整大小的List接口实现。

List are an ordered type. 列表是有序类型。 Therefore (from what I understand) you cannot just remove an item at random within the list. 因此(根据我的理解)你不能只是在列表中随机删除一个项目。 It would change the order. 它会改变顺序。

This here, for example does work : 这在这里,例如工作:

public class Ch11Ex7 {
    public static void main(String[] args){ 

        Eval e1 = new Eval();
        Eval e2 = new Eval();
        Eval e3 = new Eval();
        Eval e4 = new Eval();
        Eval e5 = new Eval();

        Eval[] eva = {e1, e2, e3, e4, e5};

        ArrayList<Eval> ev = new ArrayList<Eval>(Arrays.asList(eva));
        ArrayList<Eval> sub = new ArrayList<Eval>(ev.subList(1, 3));  
        printList(ev);
        System.out.println("Sublist");  
        printList(sub);   
        System.out.println("Remove element");

        sub.remove(1);

        System.out.println("New List"); 
        printList(ev);
        System.out.println("Sublist"); 
        printList(sub);
    }

    public static void printList(List<Eval> list) {
        for(int i=0; i< list.size() ; i++)
            System.out.println(list.get(i));   
    }
}

I have allowed myself a few changes to code style... but all in all this works (and tested) 我已经允许自己对代码风格进行一些更改......但总而言之这些工作(并经过测试)

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM