Uses of Array List

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • BigDaddyLH
    Recognized Expert Top Contributor
    • Dec 2007
    • 1216

    #16
    Originally posted by Energizer100
    I would tally each number that appeared and then see which one appeared the most.
    It's starting to sound like an algorithm, because you've broken it down into two phases:
    1. Tally each number
    2. Then see which one(s) appears the most

    Comment

    • Energizer100
      New Member
      • Nov 2007
      • 60

      #17
      Originally posted by BigDaddyLH
      It's starting to sound like an algorithm, because you've broken it down into two phases:
      1. Tally each number
      2. Then see which one(s) appears the most
      Heh, yeah.

      But my idea, in code, for it is to make different variables to store each number. But that would take forever.

      Comment

      • BigDaddyLH
        Recognized Expert Top Contributor
        • Dec 2007
        • 1216

        #18
        Originally posted by Energizer100
        Heh, yeah.

        But my idea, in code, for it is to make different variables to store each number. But that would take forever.
        You need 100 variables, one each to hold the tally count for that data value. Now if there was only an easy way to declare variables in bulk... Hmmm.... anything ring a bell... declare 100 at a time...

        Comment

        • Energizer100
          New Member
          • Nov 2007
          • 60

          #19
          Originally posted by BigDaddyLH
          You need 100 variables, one each to hold the tally count for that data value. Now if there was only an easy way to declare variables in bulk... Hmmm.... anything ring a bell... declare 100 at a time...

          i was thinking about using an array.... would that help

          Comment

          • BigDaddyLH
            Recognized Expert Top Contributor
            • Dec 2007
            • 1216

            #20
            Originally posted by Energizer100
            i was thinking about using an array.... would that help
            Yes, indeed. Why not try using an array.

            Comment

            • BigDaddyLH
              Recognized Expert Top Contributor
              • Dec 2007
              • 1216

              #21
              Originally posted by BigDaddyLH
              Yes, indeed. Why not try using an array.
              Although, if the point of this exercise is to use lists, you could also using a list like an ArrayList instead.

              Comment

              • Energizer100
                New Member
                • Nov 2007
                • 60

                #22
                I see. Yeah, ArrayLists are much easier to use.

                Comment

                • BigDaddyLH
                  Recognized Expert Top Contributor
                  • Dec 2007
                  • 1216

                  #23
                  Originally posted by Energizer100
                  I see. Yeah, ArrayLists are much easier to use.
                  My knee-jerk reaction is to use the collection framework over an array in all but trivial cases, because the collection framework is more flexible.

                  But even so, sometimes the array code is simpler:
                  [CODE=Java]import java.util.*;

                  public class Comparison {
                  private static final int MAX = 10;

                  public static void main(String[] args) {
                  int[] v = new int[MAX]; //MAX zeroes
                  List<Integer> list = new ArrayList<Integ er>(Collections . nCopies(MAX, 0)); //MAX zeroes

                  //increment the values at odd offsets
                  for(int i=1; i<v.length; i+=2) {
                  v[i]++;
                  }

                  //increment the values at odd offsets
                  for(int i=1; i<list.size(); i+=2) {
                  list.set(i, 1 + list.get(i));
                  }

                  System.out.prin tln(Arrays.toSt ring(v));
                  System.out.prin tln(list);
                  }
                  }[/CODE]

                  Comment

                  Working...