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 139 views

3 answers to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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,050 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,040 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
35 views
0 votes
1 answer

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

Please check the below code to arrive ...READ MORE

answered Nov 21, 2018 in Java by Frankie
• 9,710 points
55 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,040 points
23 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,040 points
208 views
0 votes
1 answer

What is the difference between Set and List in java?

List is an ordered sequence of elements. ...READ MORE

answered Apr 26, 2018 in Java by Akrati
• 3,150 points
34 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,520 points
38 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,840 points
278 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
28 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,310 points
27 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,050 points
49 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.