如何转换ArrayList< integer> arrayList< string>?

发布于 2025-01-26 18:46:22 字数 1399 浏览 3 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

美人迟暮 2025-02-02 18:46:22
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
    
List<String> stringVals = numbers.stream().map(a -> a.toString()).collect(Collectors.toList());
stringVals.stream().forEach(System.out::println);
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
    
List<String> stringVals = numbers.stream().map(a -> a.toString()).collect(Collectors.toList());
stringVals.stream().forEach(System.out::println);
鯉魚旗 2025-02-02 18:46:22

您用于转换的代码很好:

ArrayList<String> numens = new ArrayList<String>();
for (int  myInt : numen) { 
    numens.add(String.valueOf(myInt)); 
}

如 @ryan的答案所述,您也可以使用Java 8+流进行此操作。但是(IMO)这并不是一个重大改进。

注意:如果您正在寻找一种不创建新列表的方法来执行此操作,那么在Java中是不可能的。


但是,困扰我的事情是您这样做的合理化:

我必须转换整数 arrayList string> string arraylist so jtext在秋千上可以打印String arraylist

的数字

首先,没有jtext类型。有一个jtextfieldjtextarea

其次,这些类都没有接受列表填充字段或区域。

因此,

如果您只是从列表中填充单个字段 /区域,则必须将列表转换为字符串。而且,如果您这样做,则无需将arrayList&lt; gt;转换为arrayList&lt; string&gt;来执行此操作。例如,只需在arraylist&lt; integer&gt;上调用toString(),将其格式化它,以便将其插入jtextfieldjtextarea

如果您只是从列表中填充列表或字段 /区域数组,则必须迭代列表才能完成。那时,您还可以很好地迭代原始arraylist&lt; integer&gt;

简而言之,您的问题似乎是 xy问题

The code you are using for the conversion is fine:

ArrayList<String> numens = new ArrayList<String>();
for (int  myInt : numen) { 
    numens.add(String.valueOf(myInt)); 
}

As explained, in @Ryan's answer, you could also do this using Java 8+ streams. But (IMO) that is not a significant improvement.

Note: if you are looking for a way to do this without creating a new list, that is not possible in Java.


But the thing that puzzles me is your rationalization for doing this:

I have to convert an Integer Arraylist to String Arraylist so JText in Swing can print the numbers of the String ArrayList.

Firstly, there is no JText type. There is a JTextField or a JTextArea.

Secondly, neither of these class accept a List to populate the field or area.

So ...

If you are just populating a single field / area from a list, you are going to have to convert the list to a string. And if you are doing that, you don't need to convert an ArrayList<Integer> to an ArrayList<String> to do it. For example, just call toString() on the ArrayList<Integer> will format it so that it can be inserted into a JTextField or JTextArea.

If you are just populating a list or array of fields / areas from the list, you are going to have to iterate the list to do it. At that point, you may as well iterate the original ArrayList<Integer>.

In short, your question appears to be an XY problem.

尾戒 2025-02-02 18:46:22

toString

如果您的目标是整数的文本表示,只需调用 AbstractCollection#tostring 在您的原始arraylist上整数&gt;。无需创建另一个列表。

List< Integer > integers = List.of( 1 , 2 , 3 ) ;
String output = integers.toString() ;

请参阅此 code在indeone.com 中实时运行。

[1,2,3]

流#地图

如果您的目标是生成文本,请使用流以Newlines生成文本。调用流#映射将我们的Integer对象的流更改为String> String对象的流。我们将它们与StringBuilder相结合,最后在末尾产生多行String对象。

List < Integer > integers = List.of( 1 , 2 , 3 );
String s =
        integers
                .stream()
                .map( integer -> integer.toString() + "\n" )
                .collect( StringBuilder :: new , StringBuilder :: append , StringBuilder :: append )
                .toString();

1

2

3

toString

If your goal is a textual representation of the integers, just call AbstractCollection#toString on your original ArrayList< Integer >. No need to create another list.

List< Integer > integers = List.of( 1 , 2 , 3 ) ;
String output = integers.toString() ;

See this code run live at Ideone.com.

[1, 2, 3]

Stream#map

If your goal is generating text, use a stream to generate text with newlines. Calling Stream#map changes our stream of Integer objects into a stream of String objects. We combine those with a StringBuilder, producing a final multi-line String object at the end.

List < Integer > integers = List.of( 1 , 2 , 3 );
String s =
        integers
                .stream()
                .map( integer -> integer.toString() + "\n" )
                .collect( StringBuilder :: new , StringBuilder :: append , StringBuilder :: append )
                .toString();

1

2

3

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文