【问题标题】:How to add new element to Varargs?如何向 Varargs 添加新元素?
【发布时间】:2012-07-04 02:46:54
【问题描述】:

我有一个方法

public boolean findANDsetText  (String Description, String ... extra ) {

在里面我想调用另一个方法并将其传递给extras,但我想将新元素(描述)添加到附加组件中。

     object_for_text = getObject(find_arguments,extra);

如何在 java 中做到这一点?代码会是什么样子?

我厌倦了容纳来自this question 的代码,但无法使其工作。

【问题讨论】:

标签: java variadic-functions


【解决方案1】:

为了扩展这里的一些其他答案,数组复制可以更快地完成

String[] newArr = new String[extra.length + 1];
System.arraycopy(extra, 0, newArr, 0, extra.length);
newArr[extra.length] = Description;

【讨论】:

    【解决方案2】:

    使用Arrays.copyOf(...)

    String[] extra2 = Arrays.copyOf(extra, extra.length+1);
    extra2[extra.length] = description;
    
    object_for_text = getObject(find_arguments,extra2);
    

    【讨论】:

      【解决方案3】:

      extra 只是一个 String 数组。因此:

      List<String> extrasList = Arrays.asList(extra);
      extrasList.add(description);
      getObject(find_arguments, extrasList.toArray());
      

      您可能需要弄乱extrasList.toArray() 的泛型类型。

      你可以更快但更详细:

      String[] extraWithDescription = new String[extra.length + 1];
      int i = 0;
      for(; i < extra.length; ++i) {
        extraWithDescription[i] = extra[i];
      }
      extraWithDescription[i] = description;
      getObject(find_arguments, extraWithDescription);
      

      【讨论】:

      • 第一个选项我得到Cannot invoke asList() on the array type String[]
      • 您的意思是使用Arrays.asList(extra) 吗? (见 Radek 的评论)。
      • 现在我得到了Cannot invoke toArray() on the primitive type boolean for Arrays.asList(extra).add(Description).toArray()
      • 将给出ArrayIndexOutOfBoundsException
      • 这将不再有效(使用 Java 8u144 测试),因为 Arrays.asList() 返回一个固定大小的列表。建议使用 Arrays.copyOf(...).
      【解决方案4】:

      你的意思是这样的吗?

      public boolean findANDsetText(String description, String ... extra)
      {
          String[] newArr = new String[extra.length + 1];
          int counter = 0;
          for(String s : extra) newArr[counter++] = s;
          newArr[counter] = description;
      
          // ...
      
          Foo object_for_text = getObject(find_arguments, newArr);
      
          // ...
      }
      

      【讨论】:

        【解决方案5】:

        就是这样……

        如下处理 Var-args...

        示例:

        在您上面的示例中,第二个参数是“String...extra”

        所以你可以这样使用:

        extra[0] = "Vivek";
        extra[1] = "Hello";
        

        或者

        for (int i=0 ; i<extra.length ; i++)
        
          {
        
                  extra[i] = value;
        
          }
        

        【讨论】:

          【解决方案6】:

          在 Java 11 中用作新列表的参数:

          List<String> templateArguments = new ArrayList<(Arrays.asList(args));
          templateArguments.add(throwable.getMessage());
          String.format(template, templateArguments.toArray());
          

          【讨论】:

            【解决方案7】:

            转换为列表并返回数组,但使用实用函数更短:

            // import com.google.common.collect.Lists;
            
            var descriptionAndExtra
                = Lists.asList(description, extra).toArray(new String[extra.length + 1]));
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2011-02-20
              • 2015-10-11
              • 2018-12-10
              • 1970-01-01
              • 2013-04-17
              • 2013-05-15
              相关资源
              最近更新 更多