lundi 25 janvier 2016

I'm having trouble understanding how to make Random methods in java. Also, what do you recommend for working with Array Lists?

"Right now, I am having a spot of trouble understanding how to make random methods of my own and array list methods of my own. I'm learning Java on Udemy and after every chapter is complete, I toy around with the methods I have learned on my own, but I'm not sure how to toy around with Random or ArrayLists. Can anybody here recommend techniques or ways of remembering?"

"It should be worth noting that I am NOT good with remembering arguments. I am pretty sure I have parameters down when it comes to calling a method, but using arguments within a method is kind of tough right now."

"For example? . get(i) or the line Random rand?"

My cousin recommended that I memorize the grammar rather than the definitions themselves, but that is proving rather difficult to know how to write without knowing what to write, you know?"

"Also, does every array list have to work with a loop? Because this is hard stuff to memorize."

import java.util.Random;

import java.util.ArrayList;

public class Chap11Part10

 {    

  public static void main(String[] args)

          {

        ArrayList<Integer> numbers = new ArrayList<Integer>();

              buildAL(numbers, 5); 

                 int max = maximum(numbers);

                     display(numbers);

                      System.out.println("The maximum value is " + numbers.get(max));

                       }

       static void display(ArrayList arr) 
       {

       for (int i = 0; i < arr.size(); i++)

            System.out.print(arr.get(i) + " "); 

                System.out.println(); 

               }

       static void buildAL(ArrayList<Integer> arr, int num) {

            Random rand = new Random(System.currentTimeMillis());

              for(int i = 0; i < num; i++)

                 arr.add(rand.nextInt(101)); 

                     }

            static int maximum(ArrayList<Integer> arr){

                  int maxPos = 0; 

                     for (int i = 1; i <arr.size(); i++)

                      if (arr.get(i) > arr.get(maxPos)) 

                        maxPos = i; 

                         return maxPos;
                              }

                       }




Aucun commentaire:

Enregistrer un commentaire