explanation requested for "for"
Hi All,
I came upon this revision for "for" and adapted it to my program
below:
" for(int i = 0; i<10; i++){ }"
is changed to this form:
"for(String s: inputList){ }"
and it works !!!.... is shown below Why?
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.util.List;
//source clues List and Shuffle and for revision questioned at
//http://www.vbforums.com/showthread.php?p=2186249
public class PnlArrayListShuffleDemo extends JPanel{
private int number = 0 ;
private String strNumber[] = new String [10];
static private final String newline = "\n";
JTextArea log;
public PnlArrayListShuffleDemo() {
super(new BorderLayout());
log = new JTextArea(50,20);
log.setMargin(new Insets(5,5,5,5));
log.setEditable(false);
JScrollPane logScrollPane = new JScrollPane(log);
add(logScrollPane, BorderLayout.CENTER);
System.out.println();
log.append("Panel Array List Shuffle Demo");
log.append(newline + newline);
System.out.println("These are numbers in the Array");
log.append("These are numbers in the Array");
log.append(newline);
for(int i = 0; i<10; i++){
strNumber[i] = Integer.toString(i);
number = Integer.parseInt(strNumber[i]);
System.out.println(number);
log.append(strNumber[i]+ newline );
}
System.out.println();
log.append(newline);
System.out.println("These are numbers in the List ");
log.append("These are numbers in the List");
List<String> inputList = Arrays.asList(strNumber);
log.append(newline);
for(String s: inputList){
System.out.println(s);
log.append(s + newline);
}
System.out.println();
log.append(newline);
System.out.println("These are numbers in the Shuffled List ");
log.append("These are numbers in the Shuffled List");
Collections.shuffle(inputList);
log.append(newline);
for(String s: inputList){
System.out.println(s);
log.append(s + newline);
}
}
private static void createAndShowGUI() {
// Create and set up the window.
JFrame frame = new JFrame("Panel Array List Shuffle Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Add content to the window.
frame.add(new PnlArrayListShuffleDemo());
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
UIManager.put("swing.boldMetal", Boolean.FALSE);
createAndShowGUI();
}
});
}
}