Need help on arraylist for java

Discussion in 'General Chat' started by PinkBunny, Dec 2, 2016.

  1. Teddywonka

    Teddywonka A wonka that wonks a wonk

    Joined:
    Nov 18, 2015
    Messages:
    232
    Likes Received:
    173
    Reading List:
    Link
    Yup that is an array as here are the diferences
    Array is static, arraylist is dynamic
    Array contains primitive data types while arraylist does not (its kinda mix)

    Arraylist is like this
    ArrayList arraylistobject = new ArrayList(); //no static size
    arraylistobject.add(16); // uses the word add to add things
    arraylistobject.add("I'm handsome");
     
  2. ballster147

    ballster147 Well-Known Member

    Joined:
    Nov 7, 2015
    Messages:
    174
    Likes Received:
    69
    Reading List:
    Link
    Its like a box where you divide its inside space so that you can throw muliple items inside.

    lol troll here no idea
     
  3. Alchyr

    Alchyr Master of Procrastination

    Joined:
    Mar 23, 2016
    Messages:
    644
    Likes Received:
    1,071
    Reading List:
    Link
    If it's Java, I believe ArrayList is an implementation of a list/collection based on an array. As such, it's basically just a wrapper class that manages all the resizing and insertion/removal from an array for you. You said you don't understand it, but that's far too vague a statement. It doesn't really help people know what you need help with.

    Well, here's more details I guess.

    I'm hoping you know how basic arrays work, meaning the ones defined using brackets [], because I'm gonna be explaining based on that premise.

    When you declare an array, you should be aware that you first create a reference to that array, like
    int[] thisIsAnArray;
    creates a reference to an array of integers. Since you haven't initialized it yet, it's just null, so you have to initialize it with
    thisIsAnArray = new int[10];
    which creates a new integer array of size 10, which thisIsAnArray now points to.

    The concept is similar with an ArrayList or any other class.

    ArrayList<Integer> thisIsAnArrayList;
    creates a reference to an ArrayList of integers. I would explain why you put the type of list it is within <>, but that's a more advanced concept that you will probably learn later. For now, just know that that's where you put what you want to store in your ArrayList.

    Now, you need to initialize the ArrayList.
    thisIsAnArrayList = new ArrayList<Integer>();

    Now thisIsAnArrayList points to an empty ArrayList of integers.

    To add things to the ArrayList, you use the add method.

    thisIsAnArrayList.add(5);

    Now your ArrayList contains a 5.

    Comparing this to arrays, this would be
    thisIsAnArray[0] = 5;

    Storing 5 at index 0 in the array.
    The difference between ArrayLists and arrays is that ArrayLists manage the indexes for you. If you use the add function, it will add a new value at a not yet used index.

    After adding whatever you need to the ArrayList, you probably want to look at it.
    With arrays, to access the value you just need to do
    thisIsAnArray[0] and that will give you the value in your array at index 0.
    In an ArrayList, you have to use the get method, which takes an index and returns the value at the index.
    thisIsAnArrayList.get(0) will return the value at index 0 of the list, which will again, return 5.

    There's more, but I'm bored of typing.
     
    Last edited: Dec 2, 2016
    JustaReader likes this.
  4. SoulZer0

    SoulZer0 Heaven Refining

    Joined:
    Oct 25, 2016
    Messages:
    12,478
    Likes Received:
    24,485
    Reading List:
    Link
    After reading all of this, I realized how much of a pleb I am.
     
    kazaki and a101011 like this.
  5. ballster147

    ballster147 Well-Known Member

    Joined:
    Nov 7, 2015
    Messages:
    174
    Likes Received:
    69
    Reading List:
    Link
    Array is fixed and Arraylist is flexible
     
  6. criticalmind

    criticalmind Enter Chunni Name Here

    Joined:
    Oct 20, 2015
    Messages:
    945
    Likes Received:
    656
    Reading List:
    Link
    Dude... Wrong forum
     
  7. ballster147

    ballster147 Well-Known Member

    Joined:
    Nov 7, 2015
    Messages:
    174
    Likes Received:
    69
    Reading List:
    Link
    Its like asking Liberal Arts students with Basic programming question
     
  8. SoulZer0

    SoulZer0 Heaven Refining

    Joined:
    Oct 25, 2016
    Messages:
    12,478
    Likes Received:
    24,485
    Reading List:
    Link
    It's general chat. Anything goes here. From a lost dad to your secret fetish, there's no limit here.
     
    kazaki likes this.
  9. devilmaycry1

    devilmaycry1 Well-Known Member

    Joined:
    Dec 6, 2015
    Messages:
    395
    Likes Received:
    242
    Reading List:
    Link


    This video goes through everything only 11 min
     
  10. EienMugetsuTensho

    EienMugetsuTensho [Avid Reader] [C#, C++, Python Programmer]

    Joined:
    Nov 9, 2015
    Messages:
    951
    Likes Received:
    782
    Reading List:
    Link
    It was in novel discussion.
    You have asked for help but nothing specific what about the concept do you not understand?
    @Teddywonka and @Alchyr have given good descriptions. After reading those post what you do not understand about what they have said. http://stackoverflow.com/ is a really good resource to search for help. There you need to search before asking a question.
     
    Last edited: Dec 2, 2016
    kazaki likes this.
  11. Arcturus

    Arcturus Cat, Hidden Sith Lord

    Joined:
    Jan 1, 2016
    Messages:
    9,273
    Likes Received:
    17,815
    Reading List:
    Link
    Actually with Java 8, I believe when you instantiate the arraylist, you don't need the second reference of the object type. It would look like this:
    thisIsAnArray= new ArrayList<>();
     
  12. Phi

    Phi New Member

    Joined:
    Jul 25, 2016
    Messages:
    4
    Likes Received:
    4
    Reading List:
    Link
    Mhm, that was pretty fun. More complex than Arraylists, but it's way faster too.
     
  13. DarkBlue

    DarkBlue «Darker Than Blue» «Overlord of Sleep»

    Joined:
    Mar 19, 2016
    Messages:
    815
    Likes Received:
    876
    Reading List:
    Link
    Look it up on stackoverflow
     
  14. Alchyr

    Alchyr Master of Procrastination

    Joined:
    Mar 23, 2016
    Messages:
    644
    Likes Received:
    1,071
    Reading List:
    Link
    Technically that's probably legal, but it's always better to be clear. I mean, technically you can use Objects to store almost everything, but that's probably not a good idea. Yes that's a really bad comparison, but I think you get the idea.
     
  15. Arcturus

    Arcturus Cat, Hidden Sith Lord

    Joined:
    Jan 1, 2016
    Messages:
    9,273
    Likes Received:
    17,815
    Reading List:
    Link
    You only need to put it the first time when you create the reference. You no longer need to put it twice
     
  16. EienMugetsuTensho

    EienMugetsuTensho [Avid Reader] [C#, C++, Python Programmer]

    Joined:
    Nov 9, 2015
    Messages:
    951
    Likes Received:
    782
    Reading List:
    Link
    The way I learned to do it is
    List<> name = new ArrayList<>();
    This takes advantage of polymorphism.

    I don't use Java much as I prefer c++ and c#. Java and c# are very similar I just like the features and IDE (Visual Studio) of c#
     
    Last edited: Dec 2, 2016
  17. Luxcilla

    Luxcilla Well-Known Member

    Joined:
    Feb 6, 2016
    Messages:
    284
    Likes Received:
    174
    Reading List:
    Link
    LMAO when i saw this thread..

    There are lots of sites that can help you more than here..
     
    EienMugetsuTensho likes this.
  18. Arcturus

    Arcturus Cat, Hidden Sith Lord

    Joined:
    Jan 1, 2016
    Messages:
    9,273
    Likes Received:
    17,815
    Reading List:
    Link
    Yeah that does work and it can be useful depending upon what you want to do with the data stucture
     
  19. InfiniteCanvas

    InfiniteCanvas Active Member

    Joined:
    Sep 19, 2016
    Messages:
    2
    Likes Received:
    0
    Reading List:
    Link
    Basically the same as lists, but different area of use due to different implementation (hence it is specified as arraylist and not linked list or whatever)

    Don't get confused by the prefix 'array'. That is just to enable users to identify the different implementations of lists.

    It's just a list.
     
  20. fanobody

    fanobody Well-Known Member

    Joined:
    Jun 27, 2016
    Messages:
    451
    Likes Received:
    244
    Reading List:
    Link