Return to Video

Arrays or ArrayLists - Intro to Java Programming

  • 0:00 - 0:03
    So, we have arrays and we have arraylists, and it's
  • 0:03 - 0:05
    a bit of a nuisance that we have both. Because it
  • 0:05 - 0:07
    means that we have to make a decision between them
  • 0:07 - 0:11
    whenever we want to collect elements. Generally, arraylists are more convenient
  • 0:11 - 0:13
    because they can grow and shrink. But there are a
  • 0:13 - 0:17
    few reasons why you might want to prefer arrays. First off, the
  • 0:17 - 0:22
    syntax for the brackets, is more compact. For example here,
  • 0:22 - 0:25
    we assign values one to values two and that's a bit
  • 0:25 - 0:28
    easier to read than the equivalent with arraylists, which
  • 0:28 - 0:31
    you see over here. Also, if you know exactly what
  • 0:31 - 0:35
    values you want in a collection, then arrays give you
  • 0:35 - 0:38
    an easier syntax. Here I have a string of workdays,
  • 0:38 - 0:40
    and I know exactly that I want these five
  • 0:40 - 0:43
    strings. No more, no less. I know what they are.
  • 0:43 - 0:46
    Then I can use this handy syntax, to construct an
  • 0:46 - 0:50
    array and initialize it right away. With an array list,
  • 0:50 - 0:54
    I would have to call the add method five times,
  • 0:54 - 0:58
    once for each of these strings. Another disadvantage of array lists
  • 0:58 - 1:00
    is that they don't work well with numbers. We have
  • 1:00 - 1:03
    never done that so far, but let's say we wanted to
  • 1:03 - 1:06
    collect Numbers in an array list, I'd have to do
  • 1:06 - 1:09
    something like this, and when you look carefully, you'll notice that
  • 1:09 - 1:13
    the double here, has an uppercase D. Normally, when we
  • 1:13 - 1:15
    want to have a floating point number, we write double with
  • 1:15 - 1:18
    a lowercase d, and you may recall that the
  • 1:18 - 1:22
    number types are not classes in Java. But inside, the
  • 1:22 - 1:25
    angle brackets here, I must put in a class.
  • 1:25 - 1:28
    An array list can only collect objects of a class.
  • 1:28 - 1:31
    So for every number time, There is a companion
  • 1:31 - 1:35
    class, called the wrapper class. And an object, of a
  • 1:35 - 1:38
    wrapper, just has some number inside. If you have
  • 1:38 - 1:40
    lots of numbers, say for the sake of the argument
  • 1:40 - 1:44
    you have, 1,000,000 values, it takes a lot more
  • 1:44 - 1:47
    space to store 1,000,000 wrappers each of which contains
  • 1:47 - 1:49
    a number, than it would be to use an
  • 1:49 - 1:53
    array That's contains the numbers directly. Most of the
  • 1:53 - 1:57
    time, we're not that concerned about efficiency, but when
  • 1:57 - 1:59
    you are, and need to collect a lot of
  • 1:59 - 2:03
    numbers, definitely use an array. In conclusion, use an
  • 2:03 - 2:06
    array list as a rule of thumb, unless you want
  • 2:06 - 2:10
    the nice syntax that an array gives you. You having the brackets, having the
  • 2:10 - 2:14
    initializers. Or you collect lots of numbers.
  • 2:14 - 2:16
    In that case, array lists are quite inefficient.
Titolo:
Arrays or ArrayLists - Intro to Java Programming
Descrizione:

more » « less
Video Language:
English
Team:
Udacity
Progetto:
cs046: Intro to Programming
Duration:
02:17

English subtitles

Revisioni Compare revisions