Java ArrayList 索引 [英] Java ArrayList Index

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

问题描述

int[] alist = new int [3];
alist.add("apple");
alist.add("banana");
alist.add("orange");

说我想使用ArrayList中的第二项.获得以下输出的编码是什么?

Say that I want to use the second item in the ArrayList. What is the coding in order to get the following output?

输出:

香蕉

推荐答案

ArrayList 全错了,

  • 你不能有一个整数数组并分配一个字符串值.
  • 您不能在数组中执行 add() 方法

不如这样做:

List<String> alist = new ArrayList<String>();
alist.add("apple");
alist.add("banana");
alist.add("orange");

String value = alist.get(1); //returns the 2nd item from list, in this case "banana"

索引从 0N-1 计数,其中 N 是列表的 size().

Indexing is counted from 0 to N-1 where N is size() of list.

这篇关于Java ArrayList 索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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