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 Organizing Data Arrays

More than one array element at a time?

after using the

fruit = Arrays.copyOf(fruit, 5);

It seems like you have to add the additional elements one at a time. (i.e. fruit[3] = "someFruit")

Is there a way to add more than one at a time?

Jordan Powell
Jordan Powell
Courses Plus Student 5,410 Points

if you're entering strings you could fill it using a for loop and io. This isn't a good practice though because it's not flexible code, but if you're just trying to keep up with the videos this might help since you can just copy and paste it every time. But I wouldn't use it outside of practice.

import java.util.Scanner;
public class Main {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int NumberOfStringsYouWantToAdd = 3;
        String[] arrayOfStrings = new String[NumberOfStringsYouWantToAdd];

        for (int i = 0; i < arrayOfStrings.length; i++) {
            System.out.print("Enter your String: ");
            String s = scan.nextLine();
            arrayOfStrings[i] = s;
        }
        for(String word : arrayOfStrings){
            System.out.println(word);

        }
    }
}

Jordan, thanks for taking the time to respond. I was thinking something like that was the only way. I ask because I come from a ruby and javascript background where the arrays are more fluid and it's weird learning "an actual programming language" and there being a lot of rules and workarounds to things that I would consider normal.

It wasn't to keep up with the videos, just curious if there was a better way.

Thanks again!