Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Data Structures - Retired Organizing Data Arrays

Ryan Boyd
Ryan Boyd
7,561 Points

Adding to an array in Java

In JavaScript, there was a function to add items to an existing array, i think it was ".push()". Is there any way to do this same type of method in Java besides making a new array?

1 Answer

Rob Bridges
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Rob Bridges
Full Stack JavaScript Techdegree Graduate 35,467 Points

Hey Ryan,

Java definitely expects its arrays to have a very low overhead in the amount of resources that they use, so unfortunately once an array is created we can't add items if it's already reached the capacity we created it with, unless we do as you said and create another array.

A way around this is what is called an Array List which is covered in the most recent Java Course.

It looks something like List<String> = new ArrayList<>();

In an array list you can add items to it and remove items from your list, like you would see with arrays in other programming languages.

The draw back is that it takes more resources.

Thanks hope this helps, shout at me if it doesn't.

Ryan Boyd
Ryan Boyd
7,561 Points

Definitely helps, however I'm unsure of what "resources" exactly is? Is that space in memory(like stack or heap)?

If that's right, does that mean the copy method is better for performance because the old array gets thrown into garbage?

Rob Bridges
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Rob Bridges
Full Stack JavaScript Techdegree Graduate 35,467 Points

Hey Ryan,

By resources I mean the data that each method takes in a program, the more resources the longer a program takes to work, and the more cost it is on the entire program. Heavy resource programs tend to run slow and risk errors easier.

In direct terms is resource is the amount of data that each method takes up. Everything takes up resources, variables, arrays, array lists.

In every day terms, think of resources has the money in your pocket. You are given 100 dollars to go grocery shopping that arraylist is pretty convenient, however it costs more than the array, and you still need to budget money for all of your other items.

As for things getting thrown into the garbage, it's true when you write over an array it will store the new value, but my understand is that the old one lingers for a bit.

There's actually something important called Garbage collection in Java, that in the background goes through and opens up resource space when it notices that a method hasn't been used in a while. So in terms of throwing something in the garbage, think of it more as someone unseen silently walking behind you and cleaning up pieces of paper you haven't used in a long time.

Ryan Boyd
Ryan Boyd
7,561 Points

Awesome, thanks for the explanation Rob!