Mahdi-bagvand
Mahdi-bagvand

Reputation: 1407

How optimize some code by making array?

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.

Upvotes: 0

Views: 63

Answers (1)

Larry Schiefer
Larry Schiefer

Reputation: 15775

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

Upvotes: 1

Related Questions