Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the syntax for adding multiple strings to one variable in Java?

Tags:

java

arrays

list

Okay the thing i want to do is make a variable like Line = "hey","you",Mom".

Then later I want to be able to call Line 0 and get the string "hey".

I have something like this:

String[] history = new String "hey","you","Mom";

public String getLine(int index)
{
   if (index < 0 || index >= this.history.length)
       return null;
}

But this is not working..

How do i make this list? I'm new with the syntax in java.

like image 658
Swupper Avatar asked Apr 16 '26 18:04

Swupper


2 Answers

It's

String[] history = new String[] { "hey", "you", "Mom" };
like image 182
Tudor Avatar answered Apr 18 '26 07:04

Tudor


String[] history = new String[] {"hey","you","Mom"};
String hey = history[0];

This creates a String array with size 3 initialized with the strings hey, you and Mom.

Another option is to use a List:

List<String> history = Arrays.asList("hey","you","Mom");
String hey = history.get(0);
like image 30
helpermethod Avatar answered Apr 18 '26 06:04

helpermethod



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!