Arrays – Πίνακες – ArrayList

Τι είναι τα Arrays

Τα arrays – πίνακες είναι μια συλλογή από δεδομένα του ιδίου τύπου.

Πως δηλώνουμε arrays;

Με το που δηλώσαμε το array, η Java δεσμεύει στη μνήμη 5 θέσεις με αξία 0.
Και πρέπει να ορίσουμε ανά θέση την αξία.

Μπορούμε να δηλώσουμε και να έχουμε το ίδιο αποτέλεσμα με το πιο κάτω τρόπο,
με το που οριστεί ο πίνακας δεσμεύονται στη μνήμη οι θέσεις και αρχικοποιούνται οι αξίες.

Πως διαβάζουμε αξίες;

Διαβάζουμε αξίες χρησιμοποιώντας τους δείκτες του πίνακα.

Οι δείκτες ξεκινάνε με το μηδέν. Άρα στο πιο πάνω παράδειγμα θα εκτυπωθεί η δεύτερη αξία.

Με παρόμοιο τρόπο μπορούμε να δηλώσουμε και ένα for loop ώστε να εκτυπώσει όλες τις αξίες.

Πολυδιάστατοι Πίνακες

Τι είναι οι πολυδιάστατοι πίνακες;

Πίνακες μέσα σε πίνακες. Ο πιο συνηθισμένος είναι ο δισδιάστατος, που μοιάζει με ένα πίνακα του excel

Πως δηλώνουμε ένα δισδιάστατο πίνακα;

Μπορούμε να το δηλώσουμε και με αυτή τη μορφή

Εκτύπωση αξίας από πολυδιάστατο πίνακα

Και φυσικά εκτύπωσε με for loop

Λάθη με τα index/δείκτες.

Όταν γίνει λάθος η Java εκτυπώνει το ακόλουθο, που σημένει ότι είμαστε εκτός του εύρους του πίνακα και πρέπει να προσέξουμε καλύτερα το δείκτη μας

Στο βίντεο θα δεις μια αναλυτική περιγραφή με τα συνήθη λάθη που θα αντιμετωπίσουμε συχνά όταν επεξεργαζόμαστε arrays

Εάν δεν το είδες πάτα τον ακόλουθο σύνδεσμο https://youtu.be/dILl89UQedo?si=yj3C9D6P5KwiMrHi

Ορισμός πολυδιάστατων πινάκων.

Οι πολυδιάστατοι πίνακες είναι δομές δεδομένων που επιτρέπουν την οργάνωση πληροφοριών σε πολλές διαστάσεις, δηλαδή σε διαφορετικά επίπεδα οργάνωσης. Κάθε διάσταση αντιπροσωπεύει έναν διαφορετικό τρόπο διάταξης των δεδομένων.
Για παράδειγμα, ένας πίνακας δύο διαστάσεων (2D) είναι σαν ένα τραπέζι με γραμμές και στήλες, όπου κάθε στοιχείο έχει δύο δείκτες: έναν για τη γραμμή και έναν για τη στήλη.
Ένας πίνακας τριών διαστάσεων (3D) είναι σαν να έχουμε πολλές τέτοιες γραμμές και στήλες στοιβαγμένες, με έναν επιπλέον δείκτη για το “βάθος”.
Οι πολυδιάστατοι πίνακες είναι χρήσιμοι για την αναπαράσταση δεδομένων όπως πίνακες, εικόνες, παιχνίδια ή ακόμα και πιο σύνθετες δομές, όπως δεδομένα που εξελίσσονται στον χρόνο ή σε διαφορετικές τοποθεσίες.

Πιο κάτω θα δεις το παράδειγμα με τον πολυδιάστατο πίνακα
Το παράδειγμα αναλύθηκε βήμα προς βήμα στο βίντεο.

Η εκτύπωση

Τα Arrays δεν αλλάζουν δυναμικά μέγεθος.
αν δοκιμάσεις να τα αλλάξεις τότε οι αξίες μέσα αρχικοποιούνται.

Δυναμικοί πίνακες – ArrayList

Το ArrayList παρέχει μηχανισμούς που αναπροσαρμόζουν το μέγεθος του πίνακα.
Αυτό γίνετε κατά την προσθήκη ή την αφαίρεση στοιχείων.

Το ArrayList δεν είναι το παιδί θαύμα. Έχει θέματα με την απόδοση, ειδικά όταν αφαιρούμε στοιχεία από τη μέση του πίνακα.
Και είναι γενικά αποδεκτό ότι αναλώνουν περισσότερη μνήμη. Επίσης όταν φτιάχνουμε multi thread εφαρμογές, δεν μας εγγυώνται τον συγχρονισμό.
Καλούνται οι προγραμματιστές να φτιάξουν δικό τους μηχανισμό για συγχρονισμό. Όμως όπως και οι μηχανές υψηλής απόδοσης που θέλουν περισσότερο καύσιμο, αυτή η μικρή “θυσία“ δεν είναι τίποτα μπροστά στα καλά που παρέχει.

Στο βίντεο έχω βάλει πολύ περισσότερα παραδείγματά όπως και επεξηγήσεις.

 

Αφήστε ένα Σχόλιο

Η ηλ. διεύθυνση σας δεν δημοσιεύεται. Τα υποχρεωτικά πεδία σημειώνονται με *

We use cookies to personalise content and ads, to provide social media features and to analyse our traffic. We also share information about your use of our site with our social media, advertising and analytics partners. View more
Cookies settings
Accept
Decline
Privacy & Cookie policy
Privacy & Cookies policy
Cookie name Active

Who we are

Our website address is: http://stavrosbyte.eu.

Comments

When visitors leave comments on the site we collect the data shown in the comments form, and also the visitor’s IP address and browser user agent string to help spam detection. An anonymized string created from your email address (also called a hash) may be provided to the Gravatar service to see if you are using it. The Gravatar service privacy policy is available here: https://automattic.com/privacy/. After approval of your comment, your profile picture is visible to the public in the context of your comment.

Media

If you upload images to the website, you should avoid uploading images with embedded location data (EXIF GPS) included. Visitors to the website can download and extract any location data from images on the website.

Cookies

If you leave a comment on our site you may opt-in to saving your name, email address and website in cookies. These are for your convenience so that you do not have to fill in your details again when you leave another comment. These cookies will last for one year. If you visit our login page, we will set a temporary cookie to determine if your browser accepts cookies. This cookie contains no personal data and is discarded when you close your browser. When you log in, we will also set up several cookies to save your login information and your screen display choices. Login cookies last for two days, and screen options cookies last for a year. If you select "Remember Me", your login will persist for two weeks. If you log out of your account, the login cookies will be removed. If you edit or publish an article, an additional cookie will be saved in your browser. This cookie includes no personal data and simply indicates the post ID of the article you just edited. It expires after 1 day.

Embedded content from other websites

Articles on this site may include embedded content (e.g. videos, images, articles, etc.). Embedded content from other websites behaves in the exact same way as if the visitor has visited the other website. These websites may collect data about you, use cookies, embed additional third-party tracking, and monitor your interaction with that embedded content, including tracking your interaction with the embedded content if you have an account and are logged in to that website.

Who we share your data with

If you request a password reset, your IP address will be included in the reset email.

How long we retain your data

If you leave a comment, the comment and its metadata are retained indefinitely. This is so we can recognize and approve any follow-up comments automatically instead of holding them in a moderation queue. For users that register on our website (if any), we also store the personal information they provide in their user profile. All users can see, edit, or delete their personal information at any time (except they cannot change their username). Website administrators can also see and edit that information.

What rights you have over your data

If you have an account on this site, or have left comments, you can request to receive an exported file of the personal data we hold about you, including any data you have provided to us. You can also request that we erase any personal data we hold about you. This does not include any data we are obliged to keep for administrative, legal, or security purposes.

Where your data is sent

Visitor comments may be checked through an automated spam detection service.
Save settings
Cookies settings