Searching in your browser how to print array with elements in Java? Then you are on the right website. Stick with this post because in this article I will show you the 5 different ways in which you can print array with elements in Java. So, without any delay let's jump on to the first point:
1. Using Loop (Old Method)
For example, I named it "master" and then use square brackets"[]" which is then followed by an equal sign"=".
After the equal sign, put an open curly bracket '{' and type the four words: Apple, Grapes, Mango, Watermelon(the words can be of your choice) such that each word is in double inverted commas and are separated by commas.
Then close the curly '}' bracket which is then followed by a semi-colon. So the line is:
String master[] = {"Apple","Grapes","Mango","Watermelon"};
If you directly want to print this line by using the output statement it will show an error. So, firstly you have to create a loop.
- Write the word "for" and start parentheses "("
- You have to write an integer "int" and give it a name(I named it "i") which is followed by an equal sign.
- Type 0 and put a semi-colon.
- Then you have to write the integer name (in my case it is "i") and put a less than sign "<".
- Write the string name which you have written in the previous line( for me it is 'master') and put a full stop.
- Type the word "length" followed by a semicolon and type the integer name and then use the plus sign twice"++".
- Close the parantheses ')' and then put a semi-colon. So, the line is :
for(int i = 0;i<master.length;i++);
Now you have to finally write the output statement line " System.out.println();" and within the parentheses, we have to type the string name and within square brackets, you have to type the integer name. So the line is:
System.out.println(master[i]);
Click on the run button.
Output:
ApplesGrapes
Mango
Watermelon
2. Using Arrays.asList() Method
In this method first, we have to write a String, name it(for me it was 'master'), and give it's value as we have done in the first method. As we don't have to write a loop statement we can directly proceed on to the output statement.
Type the statement "System.out.println();" and within the parentheses "()" type Arrays.asList() and within the parentheses of Arrays.asList type the string name and finally click on run option.So the line is:
System.out.println(Arrays.asList(master));
Output:
[Apples, Grapes, Mango, Watermelon]3. Enhanced for Loop Method
In the loop statement instead of creating an Integer "int", we have to create a String and give it a name(I choose the name "masterinJava") which is then followed by a column sign ":" and then the String name of the previous line(the value for the string 'masterinJava' is same as the value of the string 'master'). So, the lines are :
String master[] = {"Apples","Grapes","Mango","Watermelon"};
for(String masterinJava : master);
For the last line, I type the output statement, and inside the parentheses, I type the name of the String which I had used in the loop statement. So the line is:
System.out.println(masterinJava);
Output:
Apples
Grapes
Mango
Watermelon
4. Arrays.toString() Method
int master[] = {1,2,5};
And write the output statement "System.out.println();" and within the parentheses write the Integer or String name(in my case, it is "master") and click on run.
Output:
[1, 2, 5]
5. Arrays.deepToString() Method
To implement this method first we have to create a two-dimensional array. To create this we need to create a new Integer "int" and then use the square brackets "[]" twice which is followed by the name of the integer (I named it "inttwo") and put an equal sign "=".
Then we need to type "new int" which is followed by two square brackets"[]" and then type {{17,13,15}}. The numbers can be of your own choice. So the line:
int [] [] inttwo = new int [] [] {{17,13,15}};
For the last line, we type the output statement "System.out.println();" and within the parentheses, we need to type "Arrays.deeptoString(inttwo);. So the line is:
System.out.println(Arrays.deeptoString(inttwo));
Finally, click on the run option.
0 Comments
masterinjavaofficial@gmail.com