0

in my code i have a for that it repeat 100 time for {lg0 , lg1 , samsong0 ,...}
{lg0 , lg1 , samsong0 ,...} is list of some class that they have static List

for (int i = 0;i < lg0.brandStringList.length ; i++ )
{
    MobileInformation mobile = new MobileInformation();

    mobile.Alert_types = lg0.Alert_typesStringList[i];
    .
    .
    .
    mobile.Video = lg0.VideoStringList[i];          
}

one way for Solving problem is writting 100 for that is not good way.
for ex:
if i have {lg0 , lg1} i must write below code

for (int i = 0;i < lg0.brandStringList.length ; i++ )
{
    MobileInformation mobile = new MobileInformation();

    mobile.Alert_types = lg0.Alert_typesStringList[i];
    .
    .
    .
    mobile.Video = lg0.VideoStringList[i];          
}

for (int i = 0;i < lg1.brandStringList.length ; i++ )
{
    MobileInformation mobile = new MobileInformation();

    mobile.Alert_types = lg1.Alert_typesStringList[i];
    .
    .
    .
    mobile.Video = lg1.VideoStringList[i];          
}  

Question
How i can make a List for all Class and optimize above code by using it.

3
  • so you have 100 classes that all define the same properties (brandStringList , Alert_typesStringList, VideoStringList...)? Commented Mar 3, 2014 at 15:15
  • I understand why you would want to change that design. Can you put all values in only one class? From the code posted, I don't understand the need to separate lg1, lg0 ... Commented Mar 3, 2014 at 15:18
  • My data is big that exist in all class . so if i put them in one class size of file will be big. So i get error limited size file eclipse. Commented Mar 3, 2014 at 15:21

1 Answer 1

1

Define a base class or interface which exposes the common methods/fields you need, then make a list of the base class and iterate through the components within the class:

public class BaseInfoClass {
    public static brandStringList[];
    public static Alert_typesStringList[];
    public static VideoStringList[];
}

private LinkedList<BaseInfoClass> mAllInfo;  //  Init this as you need with lg0, etc.

public void whateverMethod() {
    for (BaseInfoClass curClass : mAllInfo) {
        for (int i = 0; i < curClass.brandStringList.length; i)) {
            MobileInformation newInfo = new MobileInformation();
            mobile.Alert_types = curClass.Alert_typesStringList[i];
            .
            .
            .
            mobile.Video = curClass.VideoStringList[i];          
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

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.