Lists are a way of storing variables. Lists can behave similar to an array, but can vary in size.
In my last posts you learned about arrays which are a foundation to the most common lists.
Lists are themselves objects and the values can be accessed with functions like:
get(int index); // Returns the object at the position index.
set(Object x, int index); // Sets the object at the position index to be that object.
add(Object x); // Puts this object at the end of the list.
remove(Object x); // Remove this object out of the list and close the thereby created gap between the two neighbouring objects`
remove(int index); // Remove the object at the position index out of the list and close the thereby created gap between the two neighbouring objects
Some languages also allow you to put objects in between two other objects, but that behaviour is usually not necessary and slow.
The most common list type stores an array of a certain length with some positions at the end of the array being empty. The number of positions filled is stored in a variable. If the all positions are filled then the list is resized by creating a new array and copying all elements to fit the new variable. If there are less than a certain number of variables in the array, the list is also resized to free memory.
To fill the gap when an element is removed it is necessary to copy a lot of the elements of the array.
An example implementation is shown below:
Task
You might have noticed that I didn't implement all functionality I described above. That will be your task.
In python you can use lists instead of arrays and again you need no type declaration so it isn't too much syntax.
Using lists in java is a bit more Complex since you need to tell the List what types it should store:
ArrayList<Object> list = new ArrayList<Object>(startingCapacity);
You can use the functionality I told you about above on that list.
There is only one problem about the java Lists: You cannot store primitives. There is a way to store primitives in the java standard library lists, but I suggest you should make your own implementation of lists like I showed above, because storing primitives in java standard library lists is slow and eats memory.
This is the end of the The Basics
. I hope you understood everything. If you didn't, don't hesitate to ask me.
In the next chapter(#2) I will show you how to make some more advanced stuff like creating a window and paint some nice graphics in java.
I really like your explanations. I especially like the use of Object x and int index. This makes the explanation so much easier to understand.
Posted using Partiko Android
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Thank you for your reply.
Now I can finally see that someone actually reads my posts.
Would you say you have learned a lot so far?
What would you want to learn in the future?
More special algorithms(like sorting) or more special structures(like the lists mentioned here) or some optimizations(how to make code faster) or something else like special applications(AI, games, others)?
What do you think about the tasks? Did you try to do them(at least in your head)?
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
As I am starting to learn Python I do not do the tasks. I have not learned a lot, but found I really liked the explanations of the list operations like:
set(Object x, int index)
As they made it much more clear what was actually happening.
I did have problems when you were trying to explain the concept, but referencing two languages and not always being clear on the differences in use between the two or only showing examples in java.
I have written basic programs in several languages thru the years and understand most of the basics shown before lists. I have not done enough programming in Python to be comfortable trying to learn any OOP concepts yet.
Posted using Partiko Android
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
As you can see I decided to get away of the 2-language-concept and chose to focus on Java which is a bit more comfortable for me because I only know a few basics in python(I didn't got any further than writing a small game out of boredom about 1+½ years ago.).
In my next post I will write a few things about the java-syntax which I hope will allow you to better understand the java-examples I show.
In the end there is no big difference between algorithms of any language. Only the Objects of the (standard) libraries are different between them.
I will keep the style of clarifying my explanations using explanatory-purpose variable-names. Although I will be using only java from now on I hope you will keep reading. I will explain other useful concepts and algorithms and I will keep trying to explain things(like lists) that most programmers normally take for granted, but that are useful/interesting to know and not too hard to understand.
Some of these will be other concepts of lists and 3d-graphics(the latter is really easier than it may seem at first).
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Yes, I will keep reading. Thanks for the engagement.
Posted using Partiko Android
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit