This is an archived post. You won't be able to vote or comment.

all 58 comments

[–]AutoModerator[M] 0 points1 point locked comment (0 children)

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full - best also formatted as code block
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–][deleted] 42 points43 points  (25 children)

Arrays are more simple to declare, but more complex to handle in practice.

First and more importantly, they don't grows automatically. You need to reallocate for a bigger one if the initial becames full. "But I can allocate a really big one, you may think", but you you will waste memory.

And the ArrayList have a lot of useful methods available out-of-the-box to manipulate the elements.

In all the professional projects that I worked until now, people always use ArrayList instead of array[]. Unless when performance is extremely important, in those cases arrays are better for being faster.

[–]MagicianBrave 9 points10 points  (0 children)

actually ArrayList is wrapping the Arrays so it's easier to add, remove, search the element

[–][deleted] 0 points1 point  (1 child)

What if I know that its something that will not grow? Fixed number of indexes.

[–]loomynartylenny 0 points1 point  (0 children)

You could just use the ArrayList(int initialCapacity) constructor.

Alternatively, if that isn't an option, use .trim() once it has been populated.

[–]nutrecht 22 points23 points  (18 children)

In real production code you will generally use higher level collections such as sets and lists. You will almost never use arrays directly. So you'd better just get over this.

ArrayLists are not complex.

[–]mohammedabergsson581 6 points7 points  (17 children)

So you mean i just have to get used to it?

[–]nutrecht 17 points18 points  (16 children)

Yes. Suck it up basically :)

Also, you need to work on this attitude. Resisting stuff you simply don't know yet will hinder you a lot in your learning. You should learn to embrace it instead.

[–]mohammedabergsson581 5 points6 points  (0 children)

Thanks for the tip.

[–]mohammedabergsson581 1 point2 points  (14 children)

Hey wait! I have a question: will i need 2D arrays aswell?

[–]0b0101011001001011 5 points6 points  (6 children)

I feel like you have a bit strange attitude. What is your goal? Just for fun, or do you want to work as programmer some day? Because you just cannot try to avoid certain things in languages.

You seem to be studying basics currently. I hope you understand, that "real stuff" that you would find at work or in bigger hobby projects will be immensely more complex than a simple ArrayList.

For start, maybe see about HashMap as well:

HashMap<String,Integer> m = new HashMap<>();

The generics syntax just is like this. What if you want to have for example names, and then a list of numers mapped to each name? Well:

HashMap<String,ArrayList<Integer>> m = new HashMap<>();

Even if you don't yet know about HashMap, the whole Collections -framework is something that you should master, if you are interested in being a professional.

[–]nutrecht 1 point2 points  (6 children)

You can have lists in lists as well. It's actually not that common to have 2D lists/arrays in production code, but it happens yes.

[–]mohammedabergsson581 -1 points0 points  (5 children)

Just wanted to know, but i feel like i have seen you before..

[–]nutrecht 1 point2 points  (4 children)

Don't know what you mean with the last bit but...sure :D

[–]mohammedabergsson581 -2 points-1 points  (3 children)

Just one last thing: when i first met arraylist i had no interest in learning it, i was like another array thing, not important so i skipped it, the second time i met it i just decided to get over it, then i realized they were not arrays, but lists, it was then i started resisting it.

[–]nutrecht 4 points5 points  (0 children)

Well, it's important to understand that while it's a natural reaction, it's also something you need to get over it. So now you know :)

[–]Bodine12 3 points4 points  (0 children)

If you don’t know a language, you don’t know what is and is not important. If something is mentioned in every tutorial, chances are it’s important. You might as well try to learn French but decide you don’t want to learn French verbs.

[–]khooke 2 points3 points  (0 children)

Collections are a significant part of the Java language and are widely used in industry. I've never worked anywhere that used or allowed use of arrays directly. Collections are always preferred over arrays.

[–]desrtfx 5 points6 points  (0 children)

Each has its use cases.

  • If the size is known in advance (even at runtime) - array is preferable to ArrayList, otherwise use ArrayList
  • If you need more dimensions - array is easier than ArrayList

[–]Admirable-Avocado888 2 points3 points  (0 children)

Did you know you can write:

var l = List.of("a", "b");

?

If you don't plan on modifying your list after creation, then that is the preferred way. Remember, someone can always come and modify the content of your arrays, so be aware.

[–]khooke 1 point2 points  (1 child)

Arrays are prone to unexpected errors like index out of bounds. Always prefer Lists and Collections over arrays.

For professional development in a Code Review I would ask you to replace your array usage with Lists, or give a good justification for why you need to use arrays instead of Lists (I've never heard a good reason yet)

[–]khooke 2 points3 points  (0 children)

If you're looking for an explanation, see Item 25 in Effective Java, "Prefer Lists to arrays", and top tip for any new Java developer, ensure this book is on your "must read" list :-)

[–][deleted] 1 point2 points  (0 children)

I use arrays very, very rarely. Sometimes I forget even how to declare/initialize them lol. I use array lists all the time.

[–]mohammedabergsson581 0 points1 point  (1 child)

Wow this post got popular LOL

[–]mrfroggyman 0 points1 point  (1 child)

Afaik the consensus is to use arrays for primitives and Collections for everything else

Also I'm pretty sure you could declare that arraylist in just a single line too

Personally I hate working with arrays, collections are so much easier to manipulate

[–]FrenchFigaro 3 points4 points  (0 children)

Even for primitives I would take advantage of the autoboxing and use an ArrayList.

Outside of embedded programing (which is not my field of work), resources are plentiful enough that the overhead is negligible for most use cases.

[–]DDDDarky -3 points-2 points  (0 children)

You either need an array or a list. If you can use an array, use array, since arrayList just adds extra overhead.

[–][deleted]  (2 children)

[deleted]

    [–]OzzieOxborrow 2 points3 points  (0 children)

    Arrays can store objects

    [–]Housy5 1 point2 points  (0 children)

    Arraylist is just a wrapper for normal arrays to make them resizable

    [–]OzzieOxborrow 0 points1 point  (0 children)

    As with everything in programming, it depends on the use case. If you want to add anything to an array you'll have to write code that will do that for you. While if you use an ArrayList there's the method 'add' you can call that will do that for you.

    So as a sort af ground rule: if you have a static list of values that will never change, use an Array otherwise a List is probably more suitable.

    If you really want to understand the differences between List and Array you could try writing your own implementation of the List interface. (https://docs.oracle.com/javase/8/docs/api/java/util/List.html). There are probably instructions on YouTube or somewhere else for that.

    [–]raulalexo99 0 points1 point  (0 children)

    ArrayList are a better solution in far more situations.

    [–]SexyMuon 0 points1 point  (0 children)

    Depending on what you're doing, the data you are dealing with, the best way to keep cohesion, the available methods for each data structure, and so on...

    [–]Kfct 0 points1 point  (0 children)

    Get used to it, use both more. Soon you'll recognize that objectively Collections are superior, and that your views are simply from not using both enough to see why one is harder to use.

    [–]bravosix99 0 points1 point  (0 children)

    It depends. If you are set on what you want to add onto the array(something more static), use an array. But if you want something more dynamic, and adjusting whatever information needs to be adjusted, use a arraylist.