Removing java-sublist

I am trying to create a list of objects and then subscriptions, and then delete all the elements in the sublist, and then display the main list again. However, when I try to remove items from a sublist, I get an error while indexoutofbounds and an unknown source are running. How to fix this to make the application work?

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); } } 
+4
source share
6 answers

Java null lists are indexed, therefore:

 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 indices 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); } 

}

+2
source

The second subList index is exceptional, so if you need elements between [1..3], you need to use:

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

Also, what you are trying to do will not work, because the List implementation returned by subList does not perform the remove operation, so you get a java.lang.UnsupportedOperationException .

Instead, you should create a sub as an ArrayList :

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

There is no element with a sub index in your sublist. Therefore, the error is self-evident.

Indexing starts at 0.

In your case

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

So, when you try to delete an item in index 2, you get an exception at runtime. If you want to remove the second item from the list, you must call

sub.remove(1)

+1
source

You want to say sub.remove(1) And you want to declare the list as ArrayList - not using Arrays.asList . Your as is code will UnsupportedOperationException because the list of arrays has a fixed length.

+1
source

As @Peter said, indexes in Java are 0-based, so you need to use:

 sub.remove(1) 

Also , with your current program, you will get an UnsupportedOperationException, since Arrays.asList () returns a list of a fixed size that does not support deletion (which will be displayed in ev, since subList () reflects the changes in the original list).

You need to use an ArrayList or a similar mutable implementation of the List interface.

+1
source

A list is an ordered type. Therefore (from what I understand) you cannot just delete an item accidentally in the list. This will change the order.

This here, for example, works:

 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 made a few code style changes ... but it all works (and verified)

Hope this helps!

+1
source

Source: https://habr.com/ru/post/1393001/


All Articles