2

I have defined a few ArrayLists that are already populated. I have the names of all of the ones I want to iterate through in an array 'tagArrays'. Is it possible to iterate through each of them in a similar logic to mine. I know this code is not going to work however I'm not sure how the code is supposed to look. This is my attempt:

These are already populated and are defined in main method.

ArrayList<String> KEYWORDS = new ArrayList<String>();
ArrayList<String> CUSTOMERS = new ArrayList<String>();
ArrayList<String> SYSTEM_DEPS = new ArrayList<String>();
ArrayList<String> MODULES = new ArrayList<String>();
ArrayList<String> DRIVE_DEFS = new ArrayList<String>();
ArrayList<String> PROCESS_IDS = new ArrayList<String>();

This is the logic I'm using

public void genericForEachLoop(POITextExtractor te) {

        final String[] tagArrays = {"KEYWORDS", "CUSTOMERS", "SYSTEM_DEPS", "MODULES", "DRIVE_DEFS", "PROCESS_IDS"};
        ArrayList<String> al = new ArrayList<String>();

        for(int i=0; i<tagArrays.length; i++) {
            System.out.println(tagArrays[i]);
            al = tagArrays[i];


            for (String item : al) {
                if (te.getText().contains(item)) {
                    System.out.println(item);
                }
            }
        }
    }

I want the for each loop to be different every time e.g. once go through KEYWORDS, then go through CUSTOMERS etc.

2
  • You cannot go from a String to a list ^^ you should use list<list<string>> Commented Jan 29, 2018 at 10:26
  • I want to use String to give al the name of an actual ArrayList so it could replace for(String item : al ) with an actual name e.g. KEYWORDS. Commented Jan 29, 2018 at 10:28

3 Answers 3

2

You cannot reference variables with string values in Java.
What you try to do could be performed with reflection.
But I don't encourage it : it is less readable, more brittle/error prone and slower as the "classical" way.

As alternative you can provide a varargs of List<String> as last parameter of the method:

public void genericForEachLoop(POITextExtractor te, String[] tagArrays, List<String>... lists ) {

    int i = 0;
    for(List<String> list : lists) {    

        System.out.println(tagArrays[i]);                   
        for (String item : list) {

            if (te.getText().contains(item)) {
                System.out.println(item);
            }

        }

     i++;
    }
}

And invoke it in this way :

genericForEachLoop(te, 
   new String[]{"KEYWORDS", "CUSTOMERS", "SYSTEM_DEPS", "MODULES", "DRIVE_DEFS", "PROCESS_IDS"},
   KEYWORDS, CUSTOMERS,SYSTEM_DEPS,MODULES,DRIVE_DEFS,PROCESS_IDS);
Sign up to request clarification or add additional context in comments.

9 Comments

Type mismatch cannot convert from element type String to List<String>
Do you know how to fix this?
@dsafas fsafasfsa And where exactly have you this issue ?
for(List<String> list : lists) { <<< here
I think that you don't use the same parameter types in your code as in my sample code. Check it please.
|
0

I have tried following things with respect to Java 8. Below is the working example of your code, I have modified some of the code. Please check.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainClass {
    public static void main(String... args) {
        String[] strings = {"KEYWORDS", "CUSTOMERS", "SYSTEM_DEPS", "MODULES", "DRIVE_DEFS", "PROCESS_IDS"};
        ArrayList<String> KEYWORDS = new ArrayList<String>();
        KEYWORDS.add("goto");
        ArrayList<String> CUSTOMERS = new ArrayList<String>();
        CUSTOMERS.add("Royal Lotus");
        ArrayList<String> SYSTEM_DEPS = new ArrayList<String>();
        SYSTEM_DEPS.add("MAC BOOK");
        ArrayList<String> MODULES = new ArrayList<String>();
        MODULES.add("TETS MODULE");
        ArrayList<String> DRIVE_DEFS = new ArrayList<String>();
        DRIVE_DEFS.add("TEST DRIVE");
        ArrayList<String> PROCESS_IDS = new ArrayList<String>();
        PROCESS_IDS.add("-15153213");


        Map<String, List<String>> mapOfLists = new HashMap<>();
        mapOfLists.put("KEYWORDS", KEYWORDS);
        mapOfLists.put("CUSTOMERS", CUSTOMERS);
        mapOfLists.put("SYSTEM_DEPS", SYSTEM_DEPS);
        mapOfLists.put("DRIVE_DEFS", DRIVE_DEFS);
        mapOfLists.put("PROCESS_IDS", PROCESS_IDS);
        mapOfLists.put("MODULES", MODULES);
        genericForEachLoop(mapOfLists, strings);
    }


    public static void genericForEachLoop(Map<String, List<String>> mapOfLists, String[] listsToIterate) {
        Arrays.stream(listsToIterate).forEach((listName -> mapOfLists.get(listName).stream().forEach(str -> System.out.println(str))));
    }
}

I have taken out the String[] and providing it as an input to method so I can change it. Only those arrays where I want to iterate I can pass them. Further more building on top of @Eran's answer I am using the Map<String, List<String> for storing all the available ArrayLists.

Please modify the code as per your need. I have tried to use the streams and foreach methods from Java8.

Comments

0

Rather creating String array you can create Array of ArrayList, which will help you to iterate dynamically like below.

   public static void main(String[] args) 
   {
    ArrayList<String> KEYWORDS = new ArrayList<String>();
    ArrayList<String> CUSTOMERS = new ArrayList<String>();
    ArrayList<String> SYSTEM_DEPS = new ArrayList<String>();
    ArrayList<String> MODULES = new ArrayList<String>();
    ArrayList<String> DRIVE_DEFS = new ArrayList<String>();
    ArrayList<String> PROCESS_IDS = new ArrayList<String>();

    final String[] tagNames = {"KEYWORDS", "CUSTOMERS", "SYSTEM_DEPS", "MODULES", "DRIVE_DEFS", "PROCESS_IDS"};
    final List<String> tagNameList=Arrays.asList(tagNames);
    final ArrayList[] tagList = { KEYWORDS, CUSTOMERS, SYSTEM_DEPS, MODULES, DRIVE_DEFS, PROCESS_IDS };

    for (ArrayList<String> list : tagList) {
        for(String str :list){
            if(str.contains(""))
            {

            }

    }
  }

1 Comment

if I do the for loop inside my method it tells me to create a local variable tagArrays.

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.