From the course: Nail Your Java Interview

Review arrays for technical interviews - Java Tutorial

From the course: Nail Your Java Interview

Start my 1-month free trial

Review arrays for technical interviews

- [Instructor] In the next part of the course, we'll be looking at various data structures that can help us organize and store our data in different ways. These are often asked in technical interview questions, because they test not only your knowledge of a given programming language, but also how you apply that knowledge to a given problem involving data. One common data structure is called an array. In Java, an array is a collection of ordered objects of the same data type. Each object in the array can be accessed by an index, starting at index zero, up to the length of the array minus one. Since items in an array are stored in directly adjacent memory locations, an array's size cannot be changed after it's allocated. Like other data structures, there are specific operations you can use to declare, allocate, initialize, add, and remove objects to an array. Let's take a look at some code. First, we declare an array. Here, we have an int array, and we've given it the name nums. This array can only contain ints, but right now the array is empty. Next, we declare another array. The difference, we are also allocating space for five items. We haven't initialized the array with any values, but there are five potential spots where values could live. So what about initializing an array? Let's say we own a pet shop, and we have pets that are currently available, and pets that are currently not available. To represent this, we've declared, allocated space for, and initialized, two string arrays, with available and unavailable pets in our pet store. In this case, cat, dog, and fish are available, and bird, rabbit, hamster, and gerbil are unavailable. With our pet arrays created and initialized, we can print out what is in these arrays with arrays.2 string. Let's run the code. We'll run main.main, and we see both the available pets and the unavailable pets in our console. Now let's say we wanted to move one pet from available to unavailable, and another pet from unavailable to available. This would require retrieving, removing, and adding items to both the available and unavailable pet arrays, and essentially swapping one animal for the other. To do this, let's determine which objects at what indices we want to swap. We'll want to swap the fish and the bird pets, so let's create some variables. We'll write int indexOfAvailablePet, and the available pet we want to swap is at index two, that's the index of the fish, and the index of the unavailablePet is at index zero, that's the index of the bird. Then we'll want to retrieve the current available and unavailable pets. To do that, we'll need to access each individual array, and use that index to retrieve the pet. So in my availablePets array, I'm going to retrieve the fish using the index of that pet. We'll save the fish in a string called availablePet. To grab the bird, we'll say String unavailablePet, we'll use the unavailable pets array, and we'll use the index of the unavailablePet to get the bird. Now we have access to the bird and fish items. To add the bird to the availablePets array, and delete the fish, we'll access the availablePets array, access the appropriate index of where we want the bird to be, and set its value to the unavailablePet, or to our bird. To add the fish to the unavailablePets array, we'll do something pretty similar. We'll access the unavailablePets array, use the index of the bird of where that is located, so that's the index of the unavailablePet, and we'll set its value to our fish, which is the availablePet. Let's grab these print statements, add them to the bottom, and see what happens. At first, we had our normal available and unavailable pets, but then after the swap, we see fish is swapped with bird, and bird is swapped with fish. In this case, we swapped two items, but what happens if you want to add something to an array that's already full without deleting anything? Ultimately, you have to copy everything to a new, bigger array, and then add your item, but this can be time consuming. Another option is to make a huge array, but then you're wasting a bunch of space, and this makes insertion and deletion much more difficult and time consuming. If you have to use arrays, and need to create more space, comparing and contrasting these options with your interviewer is a good idea. Arrays can also be used to implement other data structures. They have N dimensions and less overhead. So if you need to use a collection of items that will not change in size, arrays are the way to go. Understanding the advantages and disadvantages of each data structure can be useful for determining which data structure organizes your data in the best way for your given use case. No data structure is perfect, but some can be perfect for your use case.

Contents