The Artima Developer Community
Sponsored Link

Java Buzz Forum
how to print array in java for loop

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
how to print array in java for loop Posted: Mar 10, 2017 11:35 PM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: how to print array in java for loop
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement
  •  Arrays in java are used to hold similar data types values.
  • System.out.print() method does not print values of array.
  • In order to print array values we have different ways.
  • Normally we use for loop and by using index we will print each element inside array.
  • let us see how many ways we can able to print values of array. 

1.Print array in java using for loop
  • How to print array in java using for loop?
  • Yes we can print arrays elements using for loop.
  • Find the length of the array using array.length and take initial value as 0 and repeat until  array.length-1. 
  • Then access each index values of an array then print.
#1. Write a program to print array in java using for loop

  1. package arraysinterview;
  2.  
  3. public class PrintArray {
  4.  
  5.     /**
  6.      * How to print java array using for loop
  7.      * @author www.instanceofjava.com
  8.      */
  9.     
  10. public static void main(String [] args){
  11.         
  12.         int[] array = { 12,13,8,34,2,7,9,43,54,21};
  13.         
  14.         for (int i = 0; i < array.length; i++) {
  15.             System.out.println(array[i]);
  16.         }
  17.         
  18. }
  19.  
  20. }

Output:

  1. 12
  2. 13
  3. 8
  4. 34
  5. 2
  6. 7
  7. 9
  8. 43
  9. 54
  10. 21

2. Print string array in java using Enhanced for loop

  • From java 1.5 using enhanced for loop also we can print array values.

#2.   How to print string array in java using for each loop













    Read: how to print array in java for loop

    Topic: Java Tutorial: Java properties [How to print the property list out to the specified print writer] Previous Topic   Next Topic Topic: Java Tutorial: Java properties [How to print the property list out to the specified output stream]

    Sponsored Links



    Google
      Web Artima.com   

    Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use