0

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

2
  • Could you post the error you're getting? Commented Oct 1, 2015 at 4:21
  • What is the error. Where you declare the array ? Commented Oct 1, 2015 at 4:27

2 Answers 2

3

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;
    }
  }
Sign up to request clarification or add additional context in comments.

10 Comments

Hello Hobbit, Thanks a lot for replying. I will check and let you know
The case statement is missing break.
@akhil_mittal the last statement need not to have break statement. because there is nothing afterwards.
Well technically yes. But it does do any harm to provide break for case 2 if someone adds a next case later on that will come handy. Also default is missing which can create hard-to-find bugs in future.
@akhil_mittal dude this is what the OP needs. Also even default is also missing what do you say about that. Should I add an UnSupportedException for that?
|
1

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.

1 Comment

Thanks Akhil for replying me back. Sure i will look after the points which you have suggested.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.