I have to change every 2nd and 3d element in an array I got from the database. The database had 4 columns so these two elements should be taken out of four elements. This is a simpler code to consider:
public void displayNames() {
String[] array = {"Avon", "Berta", "Chloe", "Derek","Avon", "Berta", "Chloe", "Derek","Avon", "Berta", "Chloe", "Derek"};
for (int i = 1; i < array.length; i = i + 4) {
StringBuilder sb = new StringBuilder(array[i]);
System.out.println(sb.substring(0, 1));
}
for (int j = 2; j < array.length; j = j + 4) {
StringBuilder s = new StringBuilder(array[j]);
System.out.println(s.substring(0, 1));
}
As you can see I can extract 2nd and third elements but only using different cycles. My question is how to do it in one cycle? Should I probably use a two-dimensional array or nested loop? Will be grateful for any hint