AgentTadpole

Java arrays

Sep 27th, 2013
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. Is it easier writing this:
  2. String var0 = "asd";
  3. String var1 = "sdf";
  4. String var2 = "dfg";
  5. and calling with:
  6. var0, var1, var2
  7.  
  8. Or:
  9. String[] vars = {"asd","sdf","dfg"};
  10. and calling with:
  11. var[0], var[1], var[2].
  12.  
  13. It's less typing, looks neater and helps with categorisation.
  14. And a quote from a friend: "It's infinitely more efficient at storing, sorting, manipulating and outputing data"
  15. It's also kind of explained in this tutorial, I think: http://www.youtube.com/watch?v=L06uGnF4IpY
  16.  
  17. They're also amazing when you get to loops. Take this, for instance:
  18. String[] words = {"test","yes","no","maybe","upside down","downside up","etc."};
  19.  
  20. for(int i; i < words.length; i++) {
  21. System.out.println(words[i]);
  22. }
  23.  
  24. Run this program, and you automatically print all values in the array words[] to the screen. The alternative is:
  25. String[] words = {"test","yes","no","maybe","upside down","downside up","etc."};
  26.  
  27. System.out.println(words[0]);
  28. System.out.println(words[1]);
  29. System.out.println(words[2]);
  30. System.out.println(words[3]);
  31. System.out.println(words[4]);
  32. System.out.println(words[5]);
  33. System.out.println(words[6]);
  34.  
  35. Imagine if I wasn't even using an array to store the values, but seperate variables. I'd also have to have
  36. String word1 = "test";
  37. String word2 = "yes";
  38. etc.
  39. The second method doesn't automatically print new values either, you'd have to add a new System.out.println.
  40.  
  41. And sorry if I just confused the hell out of you, I don't know if you've done loops and such yet.
Advertisement
Add Comment
Please, Sign In to add comment