What is the syntax to declare and initialize an array in java?

+1 vote
I am unable to declare and initialize array in java. Can you please help?
Apr 11, 2018 in Java by Akrati
• 3,150 points

edited Apr 11, 2018 by Akrati 274 views

3 answers to this question.

0 votes
You can refer the following syntax for primitive data types:


int[] IntArr = new int[2];
int[] IntArr = {1,2};
int[] IntArr = new int[]{1,2};

You can refer the following syntax for classes, say string:

String[] StrArr = new String[3];
String[] StrArr = {"x","y","z"};
String[] StrArr = new String[]{"x","y","z"};

You can also declare the array first and then initialize it:



String[] StrArr;
StrArr = new String[]{"x","y","z"};










answered Apr 12, 2018 by nsv999
• 5,070 points
0 votes

One Dimensional Array

Syntax for default values:

int[] num = new int[5];
int num[] = new int[5];

Syntax with values given (variable/field initialization):

int[] num = {1,2,3,4,5};
int num[] = {1, 2, 3, 4, 5};

Multidimensional array

Declaration

int[][] num = new int[5][2];
int num[][] = new int[5][2];
answered Jul 16, 2018 by Daisy
• 8,050 points
0 votes

You can use this method:

String[] strs = IntStream.range(0, 15)  // 15 is the size
    .mapToObj(i -> Integer.toString(i))
    .toArray(String[]::new);
answered Jul 25, 2018 by samarth295
• 2,190 points

Related Questions In Java

0 votes
2 answers

What is the syntax to initialize an array?

Rather than learning un-Official websites learn from ...READ MORE

answered Aug 2, 2018 in Java by samarth295
• 2,190 points
45 views
0 votes
2 answers

In Java, what is the best way to determine the size of an object?

I happened to find a java class "jdk.nashorn.internal.ir.debug.ObjectSizeCalculator", ...READ MORE

answered Aug 19 in Java by Sirajul
• 20,040 points
86 views
0 votes
1 answer

What is the need to override the equals and hashCode methods in Java?

You must override hashCode() in every class ...READ MORE

answered Jan 2 in Java by Daisy
• 8,050 points
35 views
0 votes
2 answers

What is the use of toString method in Java and how can I use it ?

Whenever you require to explore the constructor ...READ MORE

answered Aug 23, 2018 in Java by Daisy
• 8,050 points
390 views
0 votes
2 answers

What is the difference between Set and List in java?

LIST SET Duplicates YES NO Order Ordered Depends on Implementation Positional Access YES NO List is an ordered sequence ...READ MORE

answered Aug 14 in Java by Sirajul
• 20,040 points
87 views
0 votes
1 answer

Are arrays equivalent to objects in Java ?

Yes; the Java Language Specification writes: In the Java ...READ MORE

answered May 10, 2018 in Java by Rishabh
• 3,540 points
53 views
0 votes
2 answers

How to concatenate two arrays in Java?

public <T> T[] concatenate(T[] a, T[] b) ...READ MORE

answered Jul 19, 2018 in Java by Sushmita
• 6,870 points
409 views
0 votes
2 answers

How to convert array into list in Java?

Another workaround if you use apache commons-lang: int[] ...READ MORE

answered Aug 9, 2018 in Java by samarth295
• 2,190 points
43 views
0 votes
2 answers

Array of Objects

You can also do : A[] a = ...READ MORE

answered Aug 3, 2018 in Java by sharth
• 3,320 points
32 views
0 votes
1 answer

Why do we use threads in Java?

Reasons for using Multithreading in Java: When a ...READ MORE

answered Nov 2, 2018 in Java by nsv999
• 5,070 points
63 views