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

Java 如何在类类型的数组中添加switch语句?,java,arrays,switch-statement,Java,Arrays,Switch Statement,我有一个需求,其中仅基于特定条件,我需要初始化class类型的数组。所以我尝试在类类型的数组中插入switch语句,如下所示 for (int i=0;i <testChildData.size();i++ ) { switch (testChildData.get(i)) { SyncPreferenceItem[] syncCategoryList = { case "VISIT"

我有一个需求,其中仅基于特定条件,我需要初始化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);
                    };
            }
} 

对于(int i=0;i假设:对于每个值,您将添加
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;
  }
}

假设:对于每个值,您将添加
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;
  }
}

结构是错误的,因为正确的结构是:

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;
    }
}
SyncPreferenceItem[]syncCategoryList=新的SyncPreferenceItem[testChildData.size];

对于(int i=0;i结构是错误的,因为正确的结构是:

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;
    }
}
SyncPreferenceItem[]syncCategoryList=新的SyncPreferenceItem[testChildData.size];

对于(int i=0;我可以帮你发布你得到的错误吗?错误是什么。你在哪里声明数组?你可以发布你得到的错误吗?错误是什么。你在哪里声明数组?你好,霍比特人,非常感谢你的回复。我会检查并让你知道case语句缺少break。@akhil_mittal最后一个语句不需要有break声明。因为之后什么都没有。从技术上讲是的。但是如果有人在以后添加下一个案例,那么为案例2提供休息会有任何伤害。默认值也丢失了,这可能会在将来造成很难找到的错误。@akhil_mittal dude这是OP需要的。甚至默认值也丢失了你说的什么我应该添加一个不支持的例外吗?你好,霍比特人,非常感谢你的回答。我会检查并让你知道案例陈述缺少中断。@akhil_mittal最后一个陈述不需要有中断陈述。因为之后什么都没有。技术上是的。但是如果有人dds稍后的下一个案例将派上用场。还缺少默认值,这可能会在将来产生难以发现的错误。@akhil_mittal dude这是OP需要的。甚至默认值也缺少您对此的看法。我是否应该为此添加一个不支持的默认值?感谢akhil回复我。当然,我会关注您提出的要点我已经提出了建议。谢谢Akhil回复我。当然我会考虑你提出的要点。