0

I have two list with same size but type are different.

For example :

List<String> controlColor = new ArrayList<String>();
List<Integer> brightnessValue_1 = new ArrayList<Integer>();

Now I want get the data from two list from the same position.

For example :

 int i = 0;

 for(String ledColor : controlColor) {   
  Color color = Color.parseCSS(ledColor);
  int brightnessValue = brightnessValue_1.get(i);
  ++i;
 }

In the above way I can the data from two list and from same position at a time but is there any other way to do the same thing because I wanted to avoid to use extra variable.

Shiladittya Chakraborty
  • 4,270
  • 8
  • 45
  • 94

6 Answers6

4

You can use a plain for loop:

for (int i = 0; i < Math.min(controlColor.size(), brightnessValue.size(); ++i) {
  String ledColor = controlColor.get(i);
  int brightnessValue = brightnessValue.get(i);
}

or a pair of Iterators:

Iterator<String> colorIt = colors.iterator();
Iterator<Integer> brightnessIt = brightness.iterator();
while (brightnessIt.hasNext() && colorIt.hasNext()) {
  String ledColor = colorIt.next();
  int brightnessValue = brightnessIt.next();
}

(amongst other options). The latter is preferable, because it is more efficient for list implementations like LinkedList, where the get operation is O(n), and no less efficient for implementations like ArrayList.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
3

It looks to me like you should have a class representing this pair:

public class ColorBrightness {
    String controlColor;
    int brightnessValue_1;
}

Then your two lists become a single list List<ColorBrightness>.

Jaroslaw Pawlak
  • 5,538
  • 7
  • 30
  • 57
2

How about this one:

List<String> controlColor = new ArrayList<String>();
List<Integer> brightnessValue = new ArrayList<Integer>();

for( int inx = 0; inx < controlColor.size(); ++inx )
{
controlColor.get(inx);
brightnessValue.get(inx);
}
Pavan Dittakavi
  • 3,013
  • 5
  • 27
  • 47
2

Actually the best is to have List of your object that contains related info.

public class ColorAndBrightness{
    private String color;
    private int brightness

    //getters and setters
}

Then you can have

List<ColorAndBrightness> list
...
for (ColorAndBrightness item : list){ 
     Color color = Color.parseCSS(item.getColor());
     int brightnessValue = item.getBrightness();
}

Actually you can create methods in your ColorAndBrightness class which helps you working with it.

For example converting to Color is something you can put directly into your constructor, if you want to

public class ColorAndBrightness{
    private String textColor;
    private int brightness
    private Color color;

    public ColorAndBrightness(String color, int brightness){
        this.textColor = color;
        this.brightness = brightness;
        this.color = Color.parseCSS(color);
    }

    //getters and setters
}
libik
  • 22,239
  • 9
  • 44
  • 87
0

You can go with this code, if you dont want to use an extra variable.

for(String ledColor : controlColor) {   
  Color color = Color.parseCSS(ledColor);
  int brightnessValue = brightnessValue_1.get(controlColor.indexOf(ledColor));
 }

but in this scenario controlColor should not have duplicate entries and the size of both the arraylist should always be same otherwise you will end up getting less elements or IndexOutOfBoundsException.

Shailesh Yadav
  • 1,061
  • 1
  • 15
  • 30
0

Summary

  1. define Class for data

    public class ColorData {
        private String ledColorName;
        private int brightnessValue;
    
        public String getColorName() {
            return ledColorName;
        }
    
        public Color getColor() {
            return Color.parseCSS(ledColorName);
        }
    
        public int getBrightnessValue() {
            return brightnessValue;
        }
    }
    
  2. get from List

    List<ColorData> colorList = new ArrayList<ColorData>();
    
    for(ColorData colorData : colorList) {   
        Color color = colorData.getColor();
        int brightnessValue = colorData.getBrightnessValue();
    }
    
Tom
  • 16,842
  • 17
  • 45
  • 54