我想用指数约在Java中随机指数 [英] I want to use index about randomly index in Java

查看:107
本文介绍了我想用指数约在Java中随机指数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Java。当我使用ArrayList在Java中。如果我访问索引号随机。

I use Java. when I use ArrayList in Java. if I access index number randomly.

这是更多钞票?

如果这不是更多钞票。我应该怎么办?

If this is not posible. How should I do?

例如

ArrayList<String> al = new ArrayList<String>();
al.add(100,"stackoverflow");

这是更多钞票?

推荐答案

你似乎需要的不是ArrayList的一个,但整数和字符串之间的映射:

What you seem to need isn't an arrayList but a map between integers and strings :

HashMap<Integer,String> mymap = new HashMap<Integer,String>();

所以,你可以写

mymap.put(100,"stackoverflow");

String myString = mymap.get(100);

添加到你为什么不能使用ArrayList

Adding onto why you cannot use an arraylist:

当你第一次初始化一个ArrayList,大小为零。试图比规模仍然会抛出异常IndexOutOfBounds更大的索引上添加就像一个数组会。 ArrayList的好处是,它会动态地分配更多的内存应该大小填满。

When you first initialize an ArrayList, the size is zero. Attempting to add at an index that is larger than the size will still throw an IndexOutOfBounds exception just like an array would. The benefit of the ArrayList is that it will dynamically allocate more memory should the size fill up.

此外,阵列效率不高的稀疏矩阵,即使您对管理规模,以避免错误的努力。

Besides, arrays aren't efficient for "sparse arrays" even if you make the effort to manage the size to avoid errors.

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

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