Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法获取列表的子列表_Java - Fatal编程技术网

Java 无法获取列表的子列表

Java 无法获取列表的子列表,java,Java,我试图得到一个列表的子列表,但由于某些原因,子列表只打印 4项而不是8项 String s1 = "{{ (( 4 + 5 )) }}"; System.out.println("Length of string = " + s1.length()); String[] s2 = s1.split(""); //Split the string into array ArrayList al = new ArrayList(Arra

我试图得到一个列表的子列表,但由于某些原因,子列表只打印 4项而不是8项

        String s1 = "{{ (( 4 + 5 )) }}";
        System.out.println("Length of string = " + s1.length());
        String[] s2 = s1.split(""); //Split the string into array
        ArrayList al = new ArrayList(Arrays.asList(s2)); //Convert the array into list
        al.remove(0); //remove the first occurence
        TreeSet hs = new TreeSet(al.subList(1, 8)); //Create a hashset with only first 8 elements
输出

Length of string = 17
al list =  [{, {,  , (, (,  , 4,  , +,  , 5,  , ), ),  , }, }]
length of the list= 17
al list subset =  [ , (, 4, {]
length of the al subset= 4

这是因为您使用的是集合数据类型。集合不允许重复。在您的示例中,打印的4个项目是索引1-8中仅有的4个唯一元素。

首先,您只抓取了7个项目,而不是8个。此外,您可能希望查看
TreeSet
的构造函数,以查看构造函数中重复值的情况。扰流板,他们不保留副本:-)

System.out.println(“删除后的al:+al”);
列表a2=(列表)所有子列表(1,8);
System.out.println(“a2的大小:+a2.Size());
System.out.println(“a2:+a2”);
输出:

移除后的al:[{,,(,(,,,4,,+,,5,,,,),,,},}]
a2的尺寸:7
a2:[,(,,,,,,,,,+]


任何哈希集只存储不同的项
System.out.println("al after removal: " + al);

List<String> a2 = (List<String>) al.subList(1, 8);

System.out.println("Size of a2: " + a2.size());
System.out.println("a2: " + a2);