3

I've just recently started with Java and have gotten to Arrays. from what I can tell there are two ways of creating Arrays.

The first method makes the most sense to me coming from a python background.

type[] ArrayName;

i.e.

int[] agesOfParticipants;

However a lot of resources online use a different method of creating arrays.

ArrayList<ArrayType> Name = new ArrayList<ArrayType>;

not only is this different but from what I can tell the term ArrayList is at least partially interchangeable depending on circumstance. For instance in this response ArrayList is replaced by class A, which is declared earlier.

 A<String> obj=new A<String>();

Sorry if this is all basic stuff, but I can't find anywhere that really distinguishes between the two.

Ryuzaki L
  • 37,302
  • 12
  • 68
  • 98
corvus_poe
  • 53
  • 1
  • 4
  • 2
    If it helps with your research, the "angle bracket method" is knows as *generics*. – Joe C Dec 31 '18 at 03:38
  • 1
    [`ArrayList`](https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html) is **not** an array. It is a `List`, which happens to internally use an array for the implementation, e.g. as compared with `LinkedList`, which uses linked nodes for the implementation. As a `List`, there is functionally no differences between `ArrayList` and `LinkedList`, except for performance and memory characteristics. – Andreas Dec 31 '18 at 03:38
  • *FYI:* `int[] agesOfParticipants` doesn't *create* an array. It *declares* a variable of type array. The array is created with the `new` operator (or implicitly using an array initializer or when calling a varargs method). – Andreas Dec 31 '18 at 03:42
  • Adding to the above, you use List when you don't know the size or dynamically you want to add data to it after creating it. – KiraAG Dec 31 '18 at 03:43

1 Answers1

4

In java objects are created using new keyword

creating new Integer array with size 10, array consists of square brackets []

Integer[] array = new Integer[10];
System.out.println(Arrays.toString(array)); // print array values `[..]`

creating Integer object with value 10

Integer object = new Integer(10);
System.out.println(object); // print object value 10

creating List that only holds Integer values

List<Integer> list = new ArrayList<>();
list.add(object);
System.out.println(object); // prints list with values [10]

Angular brackets <> are Generics, that are used to define homogeneous type of objects (for example list of Integers only)

Ryuzaki L
  • 37,302
  • 12
  • 68
  • 98
  • but doesn't `Integer[]` also create a homogeneous type of object in that it can only store ints? – corvus_poe Dec 31 '18 at 04:04
  • basically arrays are homogeneous objects, `Integer[]` array is also homogeneous that can only store `Integer` objects (not int's), concept call `AutoBoxing` and AutoUnBoxing`, pretty good example https://docs.oracle.com/javase/tutorial/java/data/autoboxing.html – Ryuzaki L Dec 31 '18 at 04:20
  • The question indicates that part of OP's problem is understanding the distinction between an array and an `ArrayList`. It seems counterproductive to me to describe `ArrayList list = new ArrayList<>();` as "... creating Array list that only holds `Integer` values ...". A person reading this might think that you meant "... creating Array `list` that only holds `Integer` values ...". – Dawood ibn Kareem Dec 31 '18 at 04:28
  • yup updated sir, if it seems grammatical mistake you are always welcome to edit sir @DawoodibnKareem – Ryuzaki L Dec 31 '18 at 04:30
  • It makes much better sense now, and hopefully won't confuse OP. – Dawood ibn Kareem Dec 31 '18 at 04:34
  • It sounds like OP needs to understand generics. – Chris Rollins Dec 31 '18 at 04:38
  • do you mean to add more info regarding generics or just provide him reference examples @ChrisRollins – Ryuzaki L Dec 31 '18 at 04:40
  • 1
    You're all correct in that I need to work on my terminology. I started Java today and theres a ton of things that are entirely foreign to me. Thank you guys for pointing me in the right directions. – corvus_poe Dec 31 '18 at 06:02