Adding to Arrays in Java

An error occurred trying to load this video.

Try refreshing the page, or contact customer support.

Coming up next: What is a Class in Java? - Definition & Examples

You're on a roll. Keep up the good work!

Take Quiz Watch Next Lesson
 Replay
Your next lesson will play in 10 seconds
  • 0:04 Array Review
  • 0:22 Manual Add
  • 1:16 For Loop
  • 2:00 Can't Add Once Established
  • 3:01 Lesson Summary
Save Save Save

Want to watch this again later?

Log in or sign up to add this lesson to a Custom Course.

Log in or Sign up

Timeline
Autoplay
Autoplay
Speed Speed

Recommended Lessons and Courses for You

Lesson Transcript
Instructor: Martin Gibbs

Martin has 16 years experience in Human Resources Information Systems and has a PhD in Information Technology Management. He is an adjunct professor of computer science and computer programming.

An array is a great tool for storing multiple values in a single object. However, they are not practical if you cannot add values to them. This lesson covers adding values to arrays and provides working code examples.

Array Review

As a quick review, let's look at a basic integer array of 5 elements. We'll call the variable buckets, as the elements in an array are sometimes referred to as buckets. The length function determines the size of the array - that is how many buckets there are. When the code runs, the display will output 5.


int[] buckets = new int[5];



System.out.println(buckets.length);


Manual Add

One way to add values is to manually put them in. Maybe you have a baseline that you'll be comparing against. If the array is small in size, then this is a manageable way to add the values.

The following code adds five values to our original integer array.


buckets[0] = 15;
buckets[1] = 32;
buckets[2] = 7;
buckets[3] = 105;
buckets[4] = 78;
//fill up all the buckets
//remember: Java starts counting at 0!


There is another option that only takes up one line of code. In fact, the array is even declared and filled, all in one command.

All of the values can be added with a single statement, by placing the values within curly brackets as follows:


int[] buckets = new int[] {5, 10, 15, 20, 25};
//setup array and fill one fell swoop


This example is concise: it declares the array and fills all the values on a single line of code. Sometimes, however, you may want to sacrifice space for readability. In that case, you can place each bucket on its own line of code. Take a look at the same code but placed on separate lines:


int[] buckets = new int[] {
5,
10,
15,
20,
25
};


If you don't know what the values are going to be or have a much larger array, you can also fill the values using a loop.

For Loop

The most common way to add (and retrieve) values to an array is the for loop. The for loop runs instructions a set number of times. In this case, we'll start at 0 (the first bucket) and stop at the length of the array. Remember, the length function gives us the length.

The following code adds another variable j, which helps us add values to the array. Each time we go through the for loop, we add the value of j to the bucket in the array. Next, we increase the value of j so that all buckets don't have the same value; then we print the value of each bucket.


int j = 0;
for (int i = 0; i < buckets.length; i++) {
 buckets[i] = j;
 j += 5;
 System.out.println(buckets[i]);
}


Remember that Java starts counting at zero! This rule also applies to our loop: the first time through we are at 0, so the value in the first bucket is 0. Here is the output of that code:


java arrays for loop output


To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account
Support