开发者

how to store if statement result in an array

开发者 https://www.devze.com 2023-04-12 12:24 出处:网络
how to 开发者_开发百科have these string values in an array. its giving me the output loc[j]==1 is simple loc[j]>1 is extended. but am storing this in string.. i need to store these values in string ar

how to 开发者_开发百科have these string values in an array. its giving me the output loc[j]==1 is simple loc[j]>1 is extended. but am storing this in string.. i need to store these values in string array . how do i do it

      String s = (loc[j]==1 ? "simple" : (loc[j]>1 ? "extended" : null));

this is equivalent to this code

          if(loc[j]==1)
         System.out.println("Simple");
         else
           System.out.println("extended");


  1. Make array of strings.
  2. Store strings in array.

Like so:

String[] results = new String[loc.length]; // Looks like it should be the same length as loc. If not, change it as needed.
// later on, presumably in a for loop or something
results[j] = (loc[j]==1 ? "simple" : (loc[j]>1 ? "extended" : null));


If it is single value then no need to use array. Learn how to use Java arrays (Tutorial)

String s=null;
if(loc[j]==1) //I presume that loc is an array of int
  s="Simple";
else
if(loc[j]>1)
  s="extended";
0

精彩评论

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

关注公众号