开发者

String[] array taking items out individually

开发者 https://www.devze.com 2023-02-26 20:10 出处:网络
I have a method that requires a String[] for some details but after putting these details in how do I get them out one by one?

I have a method that requires a String[] for some details but after putting these details in how do I get them out one by one?

new String[] otherDetails = {"100", "100", "This is a picture"};

now in the picture I want to set the first string as the height, the second as the width, and the third as 开发者_高级运维a description.


You refer to an element of an array by its index, like this:

height = otherDetails[0]; // 100
width = otherDetails[1]; // 100
description = otherDetails[2]; // This is a picture


You use the index to get the values

height = otherDetails[0];
width = otherDetails[1];
description = otherDetails[2];


Extract The details from array as follows :

height = otherDetails[0]; // 100
 width = otherDetails[1]; // 100
 description = otherDetails[2]; // This is a picture

Then call your method MyFunction(String heigth,String width,String description);


The index comments are probably what you're looking for, but you can also iterate through the elements using a loop.

int arraySize = stringArray.length;
for(int i=0; i<arraySize; i++){
    if(i==0)
        height = stringArray[i];  //do stuff with that index
}

This isn't the "right" approach for your particular problem, but I think it might help you understand ways that you can access items inside an array.

Side note, you could use alternative syntax:

String[] array = new String[3];
//fill in array
for(String s : array){
    //this cycles through each element which is available as "s"
}
0

精彩评论

暂无评论...
验证码 换一张
取 消