如何在 class 类型的数组中添加 switch 语句?

How to add switch statement inside array of type class?

我有要求只根据特定条件,我需要初始化class类型的数组。所以我正在尝试在 class 类型的数组中插入 switch 语句,如下所示。

 for (int i=0;i <testChildData.size();i++ )
        {
            switch (testChildData.get(i)) {
                SyncPreferenceItem[] syncCategoryList = {
                case "VISIT":
                    new SyncPreferenceItem(R.drawable.sync_visit, R.string.PrefVisits,
                            SynchronizationManager.SYNC_CATEGORY_TYPE.VISITS);
                    break;
                case "CUSTOMERS":
                    new SyncPreferenceItem(R.drawable.sync_customer, R.string.Customers,
                            SynchronizationManager.SYNC_CATEGORY_TYPE.CUSTOMERS);
                    };
            }
} 

但是我遇到了一个错误。能否请您指出正确的方向或任何其他相同的逻辑。谢谢

假设: 对于每个值,您将添加 SyncPreferenceItem 的对象。

您可以在第二个 case 语句之后添加一个 break 语句。即使这不是这里的要求,因为在该 case 语句之后您没有任何其他内容。但可以避免您以后犯错。

在 for 循环外声明和初始化数组,并使用 switch 添加对象。

syncCategoryList = new SyncPreferenceItem[testChildData.size()];
for (int i=0;i <testChildData.size();i++ ) {
  switch (testChildData.get(i)) {
    case "VISIT":
      syncCategoryList[i] = new SyncPreferenceItem(R.drawable.sync_visit, R.string.PrefVisits,
          SynchronizationManager.SYNC_CATEGORY_TYPE.VISITS);
      break;
    case "CUSTOMERS":
      syncCategoryList[i] =  new SyncPreferenceItem(R.drawable.sync_customer, R.string.Customers,
          SynchronizationManager.SYNC_CATEGORY_TYPE.CUSTOMERS);
      break;
  }
}

如果您不确定要在 for 循环中创建多少个对象,请使用 ArrayList 而不是 SyncPreferenceItem;

的简单数组
List<SyncPreferenceItem> syncCategoryList = new ArrayList<>();

  for (int i=0;i <testChildData.size();i++ ) {
    switch (testChildData.get(i)) {
      case "VISIT":
        syncCategoryList.add(new SyncPreferenceItem(R.drawable.sync_visit, R.string.PrefVisits,
            SynchronizationManager.SYNC_CATEGORY_TYPE.VISITS));
        break;
      case "CUSTOMERS":
        syncCategoryList.add(new SyncPreferenceItem(R.drawable.sync_customer, R.string.Customers,
            SynchronizationManager.SYNC_CATEGORY_TYPE.CUSTOMERS));
      break;
    }
  }

结构错误,正确的结构应该是:

SyncPreferenceItem[] syncCategoryList = new SyncPreferenceItem [testChildData.size];
for (int i=0;i <testChildData.size();i++ ) {
    switch (testChildData.get(i)) {
        case "VISIT":
            syncCategoryList[i] = new SyncPreferenceItem(R.drawable.sync_visit, R.string.PrefVisits,
                    SynchronizationManager.SYNC_CATEGORY_TYPE.VISITS);
            break;
        case "CUSTOMERS":
            syncCategoryList[i] = new SyncPreferenceItem(R.drawable.sync_customer, R.string.Customers,
                    SynchronizationManager.SYNC_CATEGORY_TYPE.CUSTOMERS);
            break;
    }
}

有以下几点值得注意:

  1. 数组中的所有元素都必须是 SyncPreferenceItem.
  2. 类型
  3. 数组的大小为 testChildData.size
  4. 每个 case 语句都应该有一个 break else 控件将跳到下一个 case。
  5. 如果没有项目是动态的,最好使用 ArrayList
  6. OP 代码中缺少默认大小写,也应添加该默认大小写,因为无法保证开关的输入始终为 VISITCUSTOMERS。如果还有其他内容,则还需要一些代码来处理该默认情况。