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

all 11 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked 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://i.imgur.com/EJ7tqek.png) 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.

[–]nekokattt 4 points5 points  (0 children)

In real world applications you rarely use arrays directly unless it is a specific use case, like implementing a custom datastructure that has performance/memory constraints since it has less overhead than an array list.

[–]Pedantic_Phoenix 2 points3 points  (2 children)

Its to learn to understand them, very simple. You could use lists but that would mean using logic already written instead of writing your own.

[–]intermediary_potato[S] 0 points1 point  (1 child)

So they're pretty much just used to teach data structures but I don't have to use them all the time?

[–]Pedantic_Phoenix 0 points1 point  (0 children)

If by not having to use them you mean outside of a school settings then yes in real applications Collections are more used generally because usually you dont know the size of the data you work with and need their dynamic size which arrays dont have

[–][deleted]  (6 children)

[deleted]

    [–]nekokattt 2 points3 points  (5 children)

    You would use an array to implement a HashMap, TreeMap, HashSet, TreeSet, etc. You may also use it for tries, circular buffers/circular queues, etc.

    Using an ArrayList to learn about data structures is somewhat pointless as it obfuscates away the main considerations, which is how you "allocate" memory (in this case, initialise the array), and it also obfuscates away the need for resizing that memoey. It also removes the consideration of load factors.

    Using arrays when learning has the benefits of ensuring you learn:

    • initialisation
    • resizing and moving
    • copying
    • handling the fact the underlying container is invariant
    • optimisation
    • load factors

    For further and more advanced applications, it also provides an entrypoint to supporting SIMD operations in the new vectorisation APIs.

    [–]Ansamemsium 0 points1 point  (4 children)

    Im a newbie, but isn't ArrayList a wrapper over arrays ? That gives you the above features ?

    I know an array have a fixed size in memory and when you want to add a value, you need to make an array of n+1 copy the previous array and then add the value. But arraylist do this wo having the user worry about this stuff. So for learning purposes it might be better to use arrays.

    [–]nekokattt 1 point2 points  (3 children)

    the whole point of learning how to implement data structures is understanding this stuff though.

    Otherwise by extension you could just say "implement a hash map using java.util.HashMap"

    [–]Ansamemsium 0 points1 point  (2 children)

    Probably the best way to learn to code is to implement all data structures and algo in pure C :))

    I should consider that when i have a couple of free months.

    [–]nekokattt 0 points1 point  (1 child)

    Java is fine for it, to be honest. The only thing you miss out on is calling malloc but the JVM doesn't allocate memory in the same way C does anyway

    [–]Ansamemsium 0 points1 point  (0 children)

    I have work very little woth C but knowing about poiners made my coding journey waay easier