Advertisement
yo2man

Arrays

Jun 20th, 2015
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.12 KB | None | 0 0
  1. // Arrays are used to store data in a structured fashion.
  2. // It's basically a list of items, any kind of items organized in a manner,
  3. // such that each item or element within it can be accessed by using a number.
  4.  
  5. // The number is known as the index of the element and
  6. // it's simply a counter of the elements that starts at zero.
  7. // And is incremented by one for each additional element in the array.
  8.  
  9. //The items in the array can be any kind of data type or object.
  10. // Our app will use an array of string objects because each fact will be
  11. // stored as text in a string.
  12.  
  13. public class FunFactsActivity extends Activity {
  14. @Override
  15.     public void onCreate(Bundle savedInstanceState) {
  16.         super.onCreate(savedInstanceState);
  17.         setContentView(R.layout.activity_fun_facts);
  18.  
  19.         final TextView factLabel = (TextView) findViewById(R.id.factTextView);
  20.         Button showFactButton = (Button) findViewById(R.id.showFactButton);
  21.         View.OnClickListener listenerName = new View.onClickListener(){
  22.                
  23.                 @Override
  24.                 public void onClick(View view) {
  25.  
  26.                 String[] facts = {                                  
  27.                         "Ants stretch when they wake up in the morning.",                                
  28.                         "Ostriches can run faster than horses.",                                        
  29.                         "Olympic gold medals are actually made mostly of silver.",                      
  30.                         "You are born with 300 bones; by the time you are an adult you will have 206.",  
  31.                         "It takes about 8 minutes for light from the Sun to reach Earth.",              
  32.                         "Some bamboo plants can grow almost a meter in just one day.",                  
  33.                         "The state of Florida is bigger than England.",                                  
  34.                         "Some penguins can leap 2-3 meters out of the water.",                          
  35.                         "On average, it takes 66 days to form a new habit.",                            
  36.                         "Mammoths still walked the earth when the Great Pyramid was being built." };    
  37.                
  38.                 String fact = "";
  39.  
  40.                 Random randomGenerator = new Random ();
  41.                 int randomNumber = randomGenerator.nextInt(facts.length);  
  42.  
  43.                 fact = facts[randomNumber];    
  44.  
  45.                 factLabel.setText(fact);
  46.                                }
  47.         };
  48.                
  49.         showFactButton.setOnClickListener(listenerName);
  50.     }
  51. }
  52. //-----------------------------------------------------------------------------------------------------------------------------------------
  53.  
  54. public class FunFactsActivity extends Activity {
  55. @Override
  56.     public void onCreate(Bundle savedInstanceState) {
  57.         super.onCreate(savedInstanceState);
  58.         setContentView(R.layout.activity_fun_facts);
  59.  
  60.     final TextView factLabel = (TextView) findViewById(R.id.factTextView);
  61.     Button showFactButton = (Button) findViewById(R.id.showFactButton);
  62.     View.OnClickListener listenerName = new View.onClickListener(){
  63.        
  64.         @Override
  65.         public void onClick(View view) {
  66.  
  67.         String[] facts = {                                  // Datatype[] nameOfArray
  68.                 "Ants stretch when they wake up in the morning.",                                //index 0
  69.                 "Ostriches can run faster than horses.",                                         //index 1
  70.                 "Olympic gold medals are actually made mostly of silver.",                       //index 2
  71.                 "You are born with 300 bones; by the time you are an adult you will have 206.",  //index 3
  72.             "It takes about 8 minutes for light from the Sun to reach Earth.",               //index 4
  73.                 "Some bamboo plants can grow almost a meter in just one day.",                   //index 5
  74.                 "The state of Florida is bigger than England.",                                  //index 6
  75.             "Some penguins can leap 2-3 meters out of the water.",                           //index 7
  76.                 "On average, it takes 66 days to form a new habit.",                             //index 8
  77.                 "Mammoths still walked the earth when the Great Pyramid was being built." };     //index 9
  78.        
  79.         String fact = "";
  80.                 Random randomGenerator = new Random ();
  81.         int randomNumber = randomGenerator.nextInt(facts.length);  // "nameOfArray.length" uses the # of elements in the array instead of the hardcoded element like "10"
  82.        
  83.         fact = facts[randomNumber];      // this syntax is saying hey, facts array, give me the element at the index a random number.  
  84. // Duh:
  85. // Now, we could have plugged in a number here instead of the random number variable
  86. // like zero would give us the first number in the array.  //fact = facts[0]
  87. // But that would always give us the same element.
  88.  
  89.         factLabel.setText(fact);
  90.                        }
  91.         };
  92.                
  93.         showFactButton.setOnClickListener(listenerName);
  94.     }
  95. }
  96.  
  97.  
  98.  
  99.  
  100. //https://teamtreehouse.com/library/build-an-android-app/coding-the-fun-facts/introduction-to-arrays
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement