简体   繁体   English

如何在数组内部循环?

[英]How to loop inside array?

[ASK] How to loop inside array? [问] 如何在数组内部循环? this is right?这是对的?

weather_data = new Weather[]
{
for (i= 0; i < listOfMenu.size(); i++) {
new Weather(R.drawable.dring1, listOfMenu.get(0)),
}
};
String[] elements = { "a", "a", "a", "a" };

for( int i = 0; i < elements.length - 1; i++)
{
String element = elements[i];
String nextElement = elements[i+1];
}

It's simple, let me show you.很简单,我给你看看。

To enter inside a array and go around with this array you use a simple for, as you did:要进入一个数组并使用这个数组,你可以使用一个简单的 for,就像你所做的那样:

for ( parameter : arrayName)
    instruction

If by example you want to print the values that are inside the array, you should do like this:例如,如果您想打印数组内的值,您应该这样做:

int[] arrayNum = {87, 68, 52, 5, 49, 83, 45, 12, 64};   /
for(int i : arrayNum) 
System.out.printf("Array Elements" + arrayNum[i]);
 }

I didn't quite understand what you are trying to do, but my answer was a direct response to your question.我不太明白您要做什么,但我的回答是对您问题的直接回答。

Short answer, you can't.简短的回答,你不能。

Long answer is that it isn't apart of the Java syntax.答案很长,它不是 Java 语法的一部分。 You have the code to loop through it right there, all you have to do is move it outside.你有代码在那里循环,你所要做的就是把它移到外面。 Check out this handy snippet:看看这个方便的片段:

    Weather[] weather_data = new Weather[listOfMenu.size()];
    for (i= 0; i <= listOfMenu.size()-1; i++) {
        weather_data[i] = new Weather(R.drawable.dring1, listOfMenu.get(i));
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM