Understanding Arrays in Java: Basics and Operations

Slide Note
Embed
Share

Java arrays are fixed-size collections that can only hold a single data type. They require manual checking for elements and cannot be sliced like Python lists. Operations such as resizing, searching, printing, comparing, and slicing arrays need to be done manually. Remember, the array size must be declared when creating it, and resizing involves creating a new array and copying contents. When comparing arrays, manual comparison is required as == won't work. Overall, Java arrays have specific rules and limitations that developers must understand to work effectively with them.


Uploaded on Oct 01, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. Module 7 Module 7 Part 4 Part 4 Arrays

  2. Arrays The basic collection type in Java is the Array. Arrays are a lot more restrictive compared to Python lists Arrays cannot change their size once they are created Arrays can only hold a single data type You must manually check if an element is inside an array Arrays cannot be sliced Basic syntax for creating an array is: type[] name = new type[size]; Reading from or writing to an array has very similar syntax name[position] = value; // stores value at position in name variable = name[position]; // reads value at position in name

  3. Arrays You must always declare the type of the array Notice that, in general, you must declare the size of the array when you create it. This size will be unchanged throughout the lifetime of the array

  4. Resizing Arrays What if you create an array but realize later on that you need a bigger array? You ll have to create a new array with the new size, and then copy over the contents of the old array. We can determine the size of an array by using .length

  5. Searching Arrays If you want to check if a particular element is inside an array, you ll need to do it manually. There is no in keyword for a if element in array: statement.

  6. Printing Arrays Printing an array simply prints out its memory representation

  7. Printing Arrays If you want to print the contents of an array, you ll have to do it manually

  8. Comparing Arrays If you want to compare if two arrays have the same contents, much like with strings, using == will not work. You ll have to compare the arrays manually

  9. Slicing Arrays If you want to slice an array, you ll have to do it manually The examples below are equivalent

  10. Arrays class The previous slides aren t strictly true Java has an Arrays class, with lots of helpful methods to interact with arrays Notice that binarySearch() will only work if the array is sorted first.

  11. ArraysLists Java does have a data structure that is similar to Python s lists, called ArrayLists The library needs to be imported before use import java.util.ArrayList; These are objects which you need to create Just like everything else in Java, you must declare what data type the ArrayList is holding ArrayList<type> name = new ArrayList<>(); You use them through their methods name.method(); ArrayLists can only ever have one dimension

  12. ArraysLists syntax and methods

  13. Multi-dimensional arrays In Python, we were able to put lists inside of lists This can be called a multi-dimensional list E.g.: storing columns inside of rows In Java, there is specific syntax for creating and interacting with multi- dimensional arrays A 2D array uses 2 square braces, a 3D array uses 3 square braces, etc. type[][] name = new type[num_of_rows][num_of_cols]; If you wish to traverse through the whole array, you ll need as many loops as there are dimensions If your array is 2D, you ll need a loop with a nested loop inside The inner loop traverses the columns and the outer loop traverses the rows For a 3D array, you ll need a loop with a nested loop inside, which itself contains a nested loop

  14. Multi-dimensional arrays

Related


More Related Content