📜  将列表的一部分作为列表访问的Java程序

📅  最后修改于: 2022-05-13 01:55:12.419000             🧑  作者: Mango

将列表的一部分作为列表访问的Java程序

列表是存储在一起以形成集合的有序元素序列。列表可以包含重复条目和空条目。列表允许我们执行基于索引的操作,即添加、删除、操作和位置访问。 Java提供了一个内置接口 << Java.util>> 来执行列表以及其他基于类的功能。

方法:

  1. 通过维护开始和结束索引的幼稚方法。
  2. 使用subList()方法。

方法一

  1. 在列表的所需索引处访问元素。
  2. 将它们添加到创建的新空列表中。
  3. 开始和结束索引值用于访问所需的列表部分。默认情况下,假定索引从 0 开始。

这种方法需要额外的空间来维护一个新列表来存储所需的子列表。

例子

Java
// Java Program to Access the Part of List as List
 
// Importing utility and input/output java classes
import java.util.*;
import java.io.*;
// Importing Iterator class
import java.util.Iterator;
 
// Class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Declaring a list
        List list1 = new ArrayList();
 
        // Adding elements to list1
        list1.add(1);
        list1.add(7);
        list1.add(8);
        list1.add(2);
        list1.add(11);
        list1.add(3);
        list1.add(66);
        list1.add(30);
 
        // Print the original List
        System.out.println("The original list contents : ");
 
        Iterator iterator = list1.iterator();
 
        // Iterating over elements using hasNext()
        // which holds true till
        // there is further more element in the List
        while (iterator.hasNext()) {
            System.out.print(iterator.next() + " ");
        }
 
        // Declaring a new List to store sublist
        List new_list = new ArrayList();
 
        // Maintaining and Setting counter to zero
        int i = 0;
 
        // Specifying the positions of the list to access
        // custom
        int start_indx = 2, end_indx = 5;
 
        // Condition check which holds true till
        // current counter value is less than size of List
        while (i < list1.size()) {
 
            // Checking if counter is in range of start and
            // end indx
            if (i >= start_indx && i <= end_indx) {
 
                // Adding element to new List
                new_list.add(list1.get(i));
            }
 
            // Incrementing counter
            i++;
        }
 
        // Print all element of List
        System.out.println();
 
        // Display message
        System.out.println("The sublist contents : ");
 
        // Iterator
        iterator = new_list.iterator();
 
        // Iterating over elements using hasNext() method
        // which holds true till further element is
        // remaining in List else returns false
        while (iterator.hasNext()) {
 
            // Print the elements of subList
            System.out.print(iterator.next() + " ");
        }
    }
}


Java
// Java Program to Access the Part of List as List
 
// Importing java utility package, and
// all classes of input/output library
import java.util.*;
import java.util.Iterator;
import java.io.*;
 
class AccessSublist {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Declaring a List of String type
        List list1 = new ArrayList();
 
        // Adding elements to above List(List1)
        // Custom inputs
        list1.add("Are");
        list1.add("you");
        list1.add("working!");
        list1.add("hard");
        list1.add("Geeeks?");
 
        // Display message
        System.out.print("The original list contents : ");
 
        // Iterator
        Iterator iterator = list1.iterator();
 
        // Iterating over elements using hasNext()
        // which holds true till there is
        // further more element present in List
        while (iterator.hasNext()) {
 
            // Print the original List
            System.out.print(iterator.next() + " ");
        }
 
        // Extracting the contents of the list
        // index between 0 and 2 (custom)
        List new_list = list1.subList(0, 3);
 
        // Print
        System.out.println();
 
        // Display message
        System.out.print("The sublist contents : ");
 
        // Iterator
        iterator = new_list.iterator();
 
        // Iterating over elements using hasNext()
        // which holds true till there is
        // further more element present in List
        while (iterator.hasNext()) {
 
            // Print the sublist(list2)
            System.out.print(iterator.next() + " ");
        }
    }
}



输出
The original list contents : 
1 7 8 2 11 3 66 30 
The sublist contents : 
8 2 11 3

方法 2: Java为我们提供了一个内置方法 sublist() 来访问属于指定索引值范围的元素。该方法由 ArrayList 包提供。

句法 :

public List subList(int fromIndex, int toIndex)

参数:

  • fromIndex = 起始索引
  • toIndex = 结束索引

返回类型:所需元素的列表。该方法访问从Index 到toIndex-1 范围内的所有元素。如果 fromIndex 等于 toIndex,则返回一个空列表。

例子:

Java

// Java Program to Access the Part of List as List
 
// Importing java utility package, and
// all classes of input/output library
import java.util.*;
import java.util.Iterator;
import java.io.*;
 
class AccessSublist {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Declaring a List of String type
        List list1 = new ArrayList();
 
        // Adding elements to above List(List1)
        // Custom inputs
        list1.add("Are");
        list1.add("you");
        list1.add("working!");
        list1.add("hard");
        list1.add("Geeeks?");
 
        // Display message
        System.out.print("The original list contents : ");
 
        // Iterator
        Iterator iterator = list1.iterator();
 
        // Iterating over elements using hasNext()
        // which holds true till there is
        // further more element present in List
        while (iterator.hasNext()) {
 
            // Print the original List
            System.out.print(iterator.next() + " ");
        }
 
        // Extracting the contents of the list
        // index between 0 and 2 (custom)
        List new_list = list1.subList(0, 3);
 
        // Print
        System.out.println();
 
        // Display message
        System.out.print("The sublist contents : ");
 
        // Iterator
        iterator = new_list.iterator();
 
        // Iterating over elements using hasNext()
        // which holds true till there is
        // further more element present in List
        while (iterator.hasNext()) {
 
            // Print the sublist(list2)
            System.out.print(iterator.next() + " ");
        }
    }
}


输出
The original list contents : Are you working! hard Geeeks? 
The sublist contents : Are you working!