如何从Java数组中获取一个包含固定字符串的对象?

huangapple go评论71阅读模式
英文:

How to get a an object with a fixed string from an array in java?

问题

我有一个对象数组,其长度可以动态改变,但最大长度始终为8。每个对象都有一个固定的字符串描述,例如:

array.get(0).getString() = apple
array.get(1).getString() = blueberry
array.get(2).getString() = banana
array.get(3).getString() = cherry
array.get(4).getString() = coconut
array.get(5).getString() = grapefruit
array.get(6).getString() = lemon
array.get(7).getString() = orange

根据我从API获取的对象数量和字符串类型,我想要隐藏或显示一个视图。我目前正在使用类似于以下的switch语句:

if (array.toArray().length == 1) {
    switch (array.get(0).getString()) {
        case "apple":
            ...
            break;
        case "blueberry":
            ...
            break;
        case "banana":
            ...
            break;
        case "cherry":
            ...
            break;
        case "coconut":
            ...
            break;
        case "grapefruit":
            ...
            break;
        case "lemon":
            ...
            break;
        case "orange":
            ...
            break;
} else if (array.toArray().length == 2) {
    switch (array.get(0).getString()) {
        ...
    }
    switch (array.get(1).getString()) {
        ...
    }
}
// 我这样做了8次,每次都必须添加一个新的switch语句。是否有简化这个过程的方法?
英文:

I have an array of objects whose length can change dynamically but the max is always 8. Every object has a fixed string which describes it, for example:

array.get(0).getString() = apple
array.get(1).getString() = blueberry
array.get(2).getString() = banana
array.get(3).getString() = cherry
array.get(4).getString() = coconut
array.get(5).getString() = grapefruit
array.get(6).getString() = lemon
array.get(7).getString() = orange

Depending on how many objects and what type of string I get from an api, I want to hide or show a View. I'm currently using a switch statement like this:

if (array.toArray().length == 1) {

        switch (array.get(0).getString()) {
            case "apple":
                ...
                break;
            case "blueberry":
                ...
                break;
            case "banana":
                ...
                break;
            case "cherry":
                ...
                break;
            case "coconut":
                ...
                break;
            case "grapefruit":
                ...
                break;
           case "lemon":
                ...
                break;
            case "orange":
                ...
                break;

        } else if (array.toArray().length == 2) {

              switch (array.get(0).getString()) {
              ...
              }

              switch (array.get(1).getString()) {
              ...
              }
        }

I do this 8 times and every time I have to add a new switch statement. Is there a way to simplify this?

答案1

得分: 2

你可以使用循环,也可以使用 Map 来避免使用 switch 语句。

List<YourCustomObject> arr = getObjectsFromAPI();
Map<String, Boolean> fruitVisibility = new HashMap<>();
fruitVisibility.put("apple", true); // 假设苹果可见
fruitVisibility.put("orange", false); // 橙子不可见
// ..... 其他水果以此类推

for (YourCustomObject obj : arr) {
  if (fruitVisibility.containsKey(obj.getString()))
    view.setVisible(fruitVisibility.get(obj.getString()));
}

或者你可以继续使用你正在尝试的方法。

for (int i = 0; i < array.size(); i++) {
  switch (array.get(i).getString()) {
    //    case ..
    // ....
  }
}
英文:

You can use loops and you can use Map's to avoid switch statements.

List&lt;YourCustomObject&gt; arr = getObjectsFromAPI();
Map&lt;String, Boolean&gt; fruitVisibility = new HashMap&lt;&gt;();
fruitVisibility.put(&quot;apple&quot;, true); // Let&#39; say apple is visible
fruitVisibility.put(&quot;orange&quot;, false); // Orange is not visible
// ..... and so on

for(YourCustomObject obj : arr){
  if(fruitVisibility.containsKey(obj.getString()))
     view.setVisible(fruitVisibility.get(obj.getString()));
}

Or you can use the way you are trying to do.

for(int i=0; i&lt;array.size(); i++){
  switch(array.get(i).getString()){
    //    case ..
    // ....
  }
}

答案2

得分: 0

你可以使用迭代器,类似下面的方式:

 Iterator iterator = list.iterator();
  while(iterator.hasNext()) {
     System.out.println(iterator.next());
  }
英文:

you can use iterators, kind of like bellow:

 Iterator iterator = list.iterator();
  while(iterator.hasNext()) {
     System.out.println(iterator.next());
  }

huangapple
  • 本文由 发表于 2020年9月14日 00:11:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/63872860.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定