【问题标题】:String is to Substring, as ArrayList is to?字符串是子字符串,就像 ArrayList 是?
【发布时间】:2014-06-29 06:02:56
【问题描述】:

在 Java 和许多其他语言中,可以通过说出类似 String.substring(begin, end) 的内容来获取字符串的一小部分。我的问题是,是否存在一种内置功能可以对 Java 中的 Lists 执行相同的操作,从原始列表返回子列表?

【问题讨论】:

  • 我猜slice。 Java有吗?还是subList

标签: arraylist substring java


【解决方案1】:

此方法称为subListexists for both array and linked lists。请注意,它返回的列表由现有列表支持,因此更新原始列表将更新切片。

【讨论】:

  • 哦,谢谢你的警告。我真的很讨厌所有这些 Java “通过引用”的废话。
  • @QuinnMcHugh 请记住,您可以通过将切片传递给构造函数来构造一个新列表。这将强制复制。
  • @Quinn:这确实是一个视图,而不是克隆。如果要克隆该切片,则必须将其复制到新对象中。确实与参考无关。
  • @hexafraction 我知道,我只是不想说object = new Object(other)object = other,对我来说似乎很合乎逻辑。
【解决方案2】:

可以在 List API 中找到答案:List#subList(int, int)(无法弄清楚如何使链接正常工作......)

但请注意,这是基础列表的 视图,因此如果您更改原始列表,您将更改子列表,并且如果您更改子列表的语义是未定义的从结构上修改原始列表。所以我想这不是你要找的东西......

如果您想要列表的结构独立子部分,我相信您必须执行以下操作:

ArrayList<something> copy = new ArrayList<>(oldList.subsection(begin, end));

但是,这将保留对子列表中原始对象的引用。如果您想要一个全新的列表,您可能必须手动克隆所有内容。

【讨论】:

    【解决方案3】:

    该方法称为 sublist,可以在 javadocs 中找到

    http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#subList(int, int)

    【讨论】:

      【解决方案4】:

      您可以使用subList(start, end)

      ArrayList<String> arrl = new ArrayList<String>();
      //adding elements to the end
      arrl.add("First");
      arrl.add("Second");
      arrl.add("Third");
      arrl.add("Random");
      arrl.add("Click");
      System.out.println("Actual ArrayList:"+arrl);
      List<String> list = arrl.subList(2, 4);
      System.out.println("Sub List: "+list);
      

      输出

      Actual ArrayList:[First, Second, Third, Random, Click]
      Sub List: [Third, Random]
      

      【讨论】:

        【解决方案5】:

        如果您希望它与String 的子字符串完全一样,您可能只想创建一个新方法。

        public static List<String> sub(List<String> strs, int start, int end) {
            List<String> ret = new ArrayList<>(); //Make a new empty ArrayList with String values
            for (int i = start; i < end; i++) { //From start inclusive to end exclusive
                ret.add(strs.get(i)); //Append the value of strs at the current index to the end of ret 
            }
            return ret;
        }
        
        public static List<String> sub(List<String> strs, int start) {
            List<String> ret = new ArrayList<>(); //Make a new empty ArrayList with String values
            for (int i = start; i < strs.size(); i++) { //From start inclusive to the end of strs
                ret.add(strs.get(i)); //Append the value of strs at the current index to the end of ret 
            }
            return ret;
        }    
        

        如果myStrings 是以下StringsArrayList{"do","you","really","think","I","am","addicted","to","coding"},则sub(myStrings,1,6) 将返回{"you", "really", "think", "I", "am"}sub(myStrings,4) 将返回{"I", "am", "addicted", "to", "coding"}。此外,通过执行sub(myStrings, 0),它会将myStrings 重写为新的ArrayList,这有助于解决引用问题。

        【讨论】:

          猜你喜欢
          • 2019-03-03
          • 2021-07-10
          • 2023-04-07
          • 1970-01-01
          • 2015-11-11
          • 2011-02-07
          • 1970-01-01
          • 2019-05-11
          相关资源
          最近更新 更多