coders
coders

Reputation: 731

How to add switch statement inside array of type class?

I have requirement where based on the certain condition only, i need to initialize the array of type class. So i'm trying insert switch statement inside the array of type class as below.

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

But i'm getting an error. Could you please point me in the right direction or any other logic for the same. Thank you

Upvotes: 0

Views: 101

Answers (2)

akhil_mittal
akhil_mittal

Reputation: 24157

The structure is wrong as correct structure would be:

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

There are following points worth noting:

  1. All elements in the array has to be of type SyncPreferenceItem.
  2. The size of the array would be testChildData.size.
  3. Every case statement should have a break else control will skip to next case.
  4. In case no of items are dynamic it is better to use ArrayList.
  5. The default case is missing in the code of OP that should also be added becuase there is no guarantee that inputs to switch will always be VISIT and CUSTOMERS. If anything else goes in there needs to be some code to handle that default case as well.

Upvotes: 1

YoungHobbit
YoungHobbit

Reputation: 13402

Assumption: For each value you will add the object of SyncPreferenceItem.

You can add a break statement after the second case statement. Even though it is not a requirement here, because you don't have anything else after that case statement. But can save you from future errors.

Declare and initialize array outside for loop and add the object using 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;
  }
}

If you are not sure how many object you are going to create inside the for loop then use ArrayList instead of simple array of 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;
    }
  }

Upvotes: 3

Related Questions