得到的ArrayList HashMap的多个随机值 [英] get multiple random values from ArrayList HashMap

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

问题描述

我想从ArrayList中得到一些具体的数字随机值

I want to get some specific number random values from ArrayList

final ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();

for (int i = 0; i == 4; i++) {

        index = random.nextInt(menuItems.size());
        HashMap<String, String> getitem = menuItems.get(index);
        System.out.println(getitem.get(KEY_NAME));
    }

没有什么是打印输出。

Nothing is printing out.

在循环code工作,如果我用它外环线,但因为我需要多个值,我用的循环,它不工作。

Code in loop works if i use it outside loop, but since i need multiple values, i use loop and it doesnt work.

推荐答案

改变

for (int i = 0; i == 4; i++) { // start with i beeing 0, execute while i is 4
                               // never true

for (int i = 0; i < 4; i++) {  // start with i beeing 0, execute while i is
                               // smaller than 4, true 4 times

说明:

一个for循环结构如下:

Explanation:

A for loop has the following structure:

for (initialization; condition; update)

初始化在循环开始前执行一次。 条件的每次循环之前进行检查和更新每次迭代后执行。

initialization is executed once before the loop starts. condition is checked before each iteration of the loop and update is executed after every iteration.

您初始化是 INT I = 0; (执行一次)。你的条件是我== 4 ,这是假的,因为 I 0 。这样的条件为假,并且该环将被跳过。

Your initialization was int i = 0; (executed once). Your condition was i == 4, which is false, because i is 0. So the condition is false, and the loop is skipped.

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

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