What does the second line inside this for loop means and what will be the codes representation in java?
private List<int> channels = new List<int>();
private List<byte> packet= new List<int>();
for (i = 0; i < 2; i++)
{
channels.Add((int)packet[2 + (2 * i)]);
channels[i] += ((int)packet[2 + (2 * i) + 1] << 8) & 0xFF00;
}
Is this correct way to do above code in java?
for (i = 0; i < 2; i++) {
channels.add(packet.get(2 + (2 * i)));
byte temp=channels.get(i);
temp+=((packet.get(2 + (2 * i) + 1) << 8) & 0xFF00);
channels.set(i, temp);
}
Above worked fine but, this would do things better. Thanks to every one for response:
for (i = 0; i < 2; i++) {
channels.set(i, (byte) (packet.get(2 + (2 * i))+((packet.get(2 + (2 * i) + 1) << 8) & 0xFF00)));
}
What’s missing from Java is operator overloading. As such, if you use an
ArrayList<Integer>, you’ll need to useget()andset()instead of the[]operator.Additionally, I don’t think you could have assigned a
List<int>to aList<byte>in C#.