且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将字符串添加到 ArrayList

更新时间:2023-02-23 09:51:50

你做得很好,我没有看到问题,但我认为 arraylists 存储对象,所以如果你想要一个整数,请构建一个对象.我想如果你能编译它,这会自动完成.

You're doing ok, I don't see a question but I think that arraylists store objects, so if you want an integer, get build an object. I guess if you can compli that, this is done automatically.

Integer i = new Integer(1);
myList.add(i);

或在 1 行中

myList.add(new Integer(1));

正如 Paul Bellora 所说,new Integer(i) 是不必要的,你可以只用 i 替换它.我只是想指出 ArrayLists store objects(不知何故我忘了提到这一点),而不是像 int 这样的原始数据类型,当你尝试这样做时,数据是如果可能的话(对编译器)转换为 Object 的子对象,就像 Integer 一样.

As Paul Bellora says, new Integer(i) is unnecessary and you can replace it with just i. I just wanted to point out that ArrayLists store objects (somehow i forgot to mention that), not primitive data types like int and when you try to do it, the data is converted if possible (to the compiler) to a child of Object just like Integer.