Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,845 questions

51,766 answers

573 users

How to remove a sublist from a List in Java

2 Answers

0 votes
import java.util.List; 
import java.util.Arrays; 
import java.util.ArrayList; 

public class MyClass {
    public static void main(String args[]) {
        List<String> list = new ArrayList<>(Arrays.asList(
        "java", 
        "c", 
        "c++", 
        "php", 
        "python", 
        "c#")); 
   
        System.out.println(list); 
   
        list.subList(1, 3).clear(); 
   
        System.out.println(list); 
    }
}
 
 
 
/*
run:
 
[java, c, c++, php, python, c#]
[java, php, python, c#]
 
*/

 



answered May 5, 2020 by avibootz
edited Aug 13, 2025 by avibootz
0 votes
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        // Create a list
        List<Integer> list = new ArrayList<>();
        for (int i = 1; i <= 10; i++) {
            list.add(i);
        }
        System.out.println("Original List: " + list);

        // Define the range to remove (fromIndex inclusive, toIndex exclusive)
        int fromIndex = 3;
        int toIndex = 7;

        // Remove the sublist
        list.subList(fromIndex, toIndex).clear();
        System.out.println("List after removing sublist: " + list);
    }
}

 
 
 
/*
run:
 
Original List: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
List after removing sublist: [1, 2, 3, 8, 9, 10]
 
*/

 



answered Aug 13, 2025 by avibootz
...