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 IntegerArraylist to StringArraylist so JText in Swing can print the numbers of the StringArrayList.
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.
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.
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.
发布评论
评论(3)
您用于转换的代码很好:
如 @ryan的答案所述,您也可以使用Java 8+流进行此操作。但是(IMO)这并不是一个重大改进。
注意:如果您正在寻找一种不创建新列表的方法来执行此操作,那么在Java中是不可能的。
但是,困扰我的事情是您这样做的合理化:
首先,没有
jtext
类型。有一个jtextfield
或jtextarea
。其次,这些类都没有接受
列表
填充字段或区域。因此,
如果您只是从列表中填充单个字段 /区域,则必须将列表转换为字符串。而且,如果您这样做,则无需将
arrayList&lt; gt;
转换为arrayList&lt; string&gt;
来执行此操作。例如,只需在arraylist&lt; integer&gt;
上调用toString()
,将其格式化它,以便将其插入jtextfield
或jtextarea
。如果您只是从列表中填充列表或字段 /区域数组,则必须迭代列表才能完成。那时,您还可以很好地迭代原始
arraylist&lt; integer&gt;
。简而言之,您的问题似乎是 xy问题。
The code you are using for the conversion is fine:
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:
Firstly, there is no
JText
type. There is aJTextField
or aJTextArea
.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 anArrayList<String>
to do it. For example, just calltoString()
on theArrayList<Integer>
will format it so that it can be inserted into aJTextField
orJTextArea
.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.
toString
如果您的目标是整数的文本表示,只需调用
AbstractCollection#tostring
在您的原始arraylist上整数&gt;
。无需创建另一个列表。请参阅此 code在indeone.com 中实时运行。
流#地图
如果您的目标是生成文本,请使用流以Newlines生成文本。调用
流#映射
将我们的Integer
对象的流更改为String> String
对象的流。我们将它们与StringBuilder
相结合,最后在末尾产生多行String
对象。toString
If your goal is a textual representation of the integers, just call
AbstractCollection#toString
on your originalArrayList< Integer >
. No need to create another list.See this code run live at Ideone.com.
Stream#map
If your goal is generating text, use a stream to generate text with newlines. Calling
Stream#map
changes our stream ofInteger
objects into a stream ofString
objects. We combine those with aStringBuilder
, producing a final multi-lineString
object at the end.