Arrays And ArrayList In Java

in #java6 years ago

codes-coding-computer-270623.jpg
Image courtesy of pexels.com

It's interesting, that on my quest to get my head around Java, there is real limited information on ArrayList and even Array's for that matter. I think this extends from the limitations that may be faced by the Array in Java, but it seems to be a major part of other programming languanges so I was surprised to see that it reletively glossed over by documentation. So, on that note, I hope this helps to provide some clarity.

Arrays in Java


So far the data types we have been using can only store one value, but with array's they can store multiple values. As an analogy, you can think of a normal variable as a house, but in this case an array would be represented as an appartment building. A house would usually only have one family living in it, which is similar to the string and integer variables we have been created. Just like an appartment building, many families live in the one building or container, this is the same for an array. Just like referencing variables, you would reference a house by the street address, within an appartment you would reference each apparment by the appartment number first and then the street address.

So, one element lives in a house, just like a normal variable, while many elements live in an appartment building, just like an array.

In an array to add and reference a value you use the name of the array and the index element, just as you would an appartment. When we create the array, instead of specifying the type of variable, we specify type[], with squarte brackets so Java knows we are creating an array.

For example, if we wanted to create a String array with 5 elements, we would write:
String[] list = new String[5];

Keep in mind that the values of our array is numbered from 0 to 4, so if we wanted to access the value of the third element, we would write:
System.out.println(list[2]);

To print to screen the number of elements in the array you would use the following function:
System.out.println(list.length);

To assign a value to an element, you can do the following:
list[1] = "Cat";

Summary of Arrays:

  • An array consists of many elements
  • To access an element in the array, you specify the element number in square brackets
  • All elements in the array will be of the same type
  • The initial value of all elements in the array is null
  • When you create an array, you need to indicate its length, or number of elements it will hold
  1 import java.io.*;
  2 
  3 public class Arrays {
  4 
  5         public static void main(String[] args) throws Exception {
  6 
  7                 Reader r = new InputStreamReader(System.in);
  8                 BufferedReader  reader = new BufferedReader(r);
  9 
 10                 //Create the array and read in the values from the keyboard
 11                 String[] list = new String[10];
 12                 for(int i = 0; i < list.length; i++){
 13                         String s = reader.readLine();
 14                         list[i] = s;
 15                 }
 16 
 17                 //List all the values in the array
 18                 for(int i = 0; i < list.length; i++){
 19                         System.out.println(list[i]);
 20                 }
 21         }
 22 }

ArrayList In Java


I am sure you can see that arrays are pretty cool and you have some good use for them, but on the other hand I am sure you can also see they do have some limtations. The main thing your probably concerned about is the fact that you cannot change an array size. This is where the ArrayList class has been created.

This is where the ArrayList class comes in as it can allow us to create an array that can change its size. Things are pretty similar to creating and working with arrays but it allows us to do some things that we couldn't do previously. This includes support for inserting and eleting elements from the middle of the array without leaving holes. An ArrayList can also change the size of the array.

To create an ArrayList, we no do this:
ArrayList<String> list = new ArrayList<String>();

To get the number of elements in the ArrayList, we can do this:
System.out.println(list.size());

To get the value of a specific element we can do the following:
System.out.println(list.get(3));

If we want to set the third element as the value of "s", we could then do:
list.set(3, s);

  1 import java.util.*;
  2 import java.io.*;
  3 
  4 public class Arrays {
  5 
  6         public static void main(String[] args) throws Exception {
  7 
  8                 Reader r = new InputStreamReader(System.in);
  9                 BufferedReader  reader = new BufferedReader(r);
 10 
 11                 //Create the arraylist and read in the values from the keyboard
 12                 ArrayList<String> list = new ArrayList<String>();
 13                 for(int i = 0; i < 10; i++){
 14                         String s = reader.readLine();
 15                         list.add(s);
 16                 }
 17 
 18                 //List all the values in the array
 19                 for(int i = 0; i < list.size(); i++){
 20                         System.out.println(list.get(i));
 21                 }
 22         }
 23 }

You need to make sure the java.util.* library is imported at the start of our code to make sure the ArrayList class is available for use to use.

If you have found this post useful or interesting, please consider Commenting, Upvoting, Following and/or Resteeming

Sort:  

you can help me on my blog I follow you and you follow me and I interact

Congratulations! This post has been upvoted from the communal account, @minnowsupport, by run.vince.run from the Minnow Support Project. It's a witness project run by aggroed, ausbitbank, teamsteem, someguy123, neoxian, followbtcnews, and netuoso. The goal is to help Steemit grow by supporting Minnows. Please find us at the Peace, Abundance, and Liberty Network (PALnet) Discord Channel. It's a completely public and open space to all members of the Steemit community who voluntarily choose to be there.

If you would like to delegate to the Minnow Support Project you can do so by clicking on the following links: 50SP, 100SP, 250SP, 500SP, 1000SP, 5000SP.
Be sure to leave at least 50SP undelegated on your account.

Dear friend, could you please write a comment at https://steemit.com/runnerpftheweek/@runningproject/runner-of-the-week-the-steem-running-project-ed7-season-2-powered-by-actifit
Actifit will reward you with AFIT Tokens and a good upvote

Coin Marketplace

STEEM 0.16
TRX 0.15
JST 0.028
BTC 56162.63
ETH 2368.26
USDT 1.00
SBD 2.31