Java的ArrayList的指数 [英] Java ArrayList Index

查看:136
本文介绍了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的都错了,

You have ArrayList all wrong,


  • 您不能有一个整数数组,并为其分配一个字符串值。

  • 您不能在一个阵列做了添加()

  • You can't have an integer array and assign a string value.
  • You cannot do a add() method in an array

而做到这一点:

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"

索引是从 0 数到 N-1 ,其中 N 尺寸()名单。

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

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