Java List.add()方法 [英] Java List.add() method

查看:164
本文介绍了Java List.add()方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是Java List Interface的方法add();如果我循环遍历它7次,则将i添加到第0个位置是这样的.

Below is the method add() of Java List Interface; if I loop through it 7 times adding i to the 0th position like so.

for (int i = 0; i < 7; i++) {
    list.add(0, i);
}

它不会覆盖该位置的值,所以我最终只会在列表中得到一个值为6的值吗?我猜对了吗?

Wouldn't it overwrite the value at that position, so I would end up with just one value of 6 in the list? Am I right, in assuming that?

推荐答案

不,如果在某个位置添加,它将使从该位置开始的所有内容向右移动.

No, if you add at a position, it shifts everything starting at that position to the right.

因此,如果您实际上是这样做的,那么您应该得到以下列表:

So if you actually did this, you should end up with the following list:

[6, 5, 4, 3, 2, 1, 0]

阅读API:或者更好,尝试一下.

这篇关于Java List.add()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆