ListsOfPrimitives.java 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import java.util.ArrayList;
  2. public class ListsOfPrimitives {
  3. public static void main( String[] args ) {
  4. ArrayList<String> hats = new ArrayList<String>();
  5. hats.add("fez");
  6. hats.add("bowler");
  7. hats.add("beanie");
  8. hats.add("western");
  9. hats.add("fedora");
  10. System.out.println( hats );
  11. String jumble = "";
  12. for ( String s : hats ) {
  13. jumble += s;
  14. }
  15. System.out.println("All together now: " + jumble);
  16. // ArrayList<int> bins = new ArrayList<int>();
  17. ArrayList<Integer> bins = new ArrayList<Integer>();
  18. bins.add(new Integer(1));
  19. bins.add(new Integer(3));
  20. bins.add(new Integer(3));
  21. bins.add(new Integer(1));
  22. bins.add(1);
  23. bins.add(4);
  24. bins.add(6);
  25. bins.add(4);
  26. bins.add(1);
  27. System.out.println( bins );
  28. int total = 0;
  29. for ( Integer N : bins ) {
  30. int n = N.intValue();
  31. total += n;
  32. }
  33. System.out.println("The total is " + total);
  34. total = 0;
  35. for ( int n : bins ) {
  36. total += n;
  37. }
  38. System.out.println("The total is still " + total);
  39. ArrayList<Character> letters = new ArrayList<Character>();
  40. letters.add('z'); // auto-boxes char
  41. ArrayList<Double> weights = new ArrayList<Double>();
  42. weights.add(0.14); // auto-boxes double
  43. ArrayList<Boolean> dealt = new ArrayList<Boolean>();
  44. while ( dealt.size() < 52 ) {
  45. dealt.add(false); // auto-boxes boolean
  46. }
  47. }
  48. }