如何在数组中存储if语句结果

时间:2011-10-13 04:57:11

标签: java

如何在数组中包含这些字符串值。它给我输出loc [j] == 1是简单的loc [j]> 1是扩展的。但我将其存储在字符串中..我需要将这些值存储在字符串数组中。我该怎么做

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

这相当于此代码

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

2 个答案:

答案 0 :(得分:1)

  1. 制作字符串数组。
  2. 将字符串存储在数组中。
  3. 像这样:

    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));
    

答案 1 :(得分:0)

如果是single值,则无需使用数组。了解如何使用Java arrays(教程)

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";