Click here to Skip to main content
14,331,456 members
Rate this:
Please Sign up or sign in to vote.
See more:
public class JavaArrays 
{                        
         public static void main(String[] args) 
         {
                        String[] Words = new String[4]; 
                        Words[0] = "Android";
                        Words[1] = "Java";
                        Words[2] = "CSS";
                        Words[3] = "JavaScript";   

                        for(String word : Words)
                        {
                        System.out.print(Words);                                               
                        System.out.println();
                        } 
}




the result is ;
[Ljava.lang.String;@52e922
[Ljava.lang.String;@52e922
[Ljava.lang.String;@52e922
[Ljava.lang.String;@52e922

should
Android
Java
CSS
JavaScript

What I have tried:

changed code several times java docs
Posted
Updated 25-Aug-19 2:09am

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

The reason you are getting 4 outputs is because you have 4 elements in the array,
for(String word : Words)
But you are printing the array itself, not the String element. Thus, Java is unable to print the array rather outputs the address where that object is being stored in the memory. You can fix that by changing the line of code that prints the words,
for(String word : Words)
{
    System.out.println(word); 
} 
Like this, now the output would be the words in the array, not the array itself.

Complete code for this would be,
public class JavaArrays {

     public static void main(String []args) {
        String[] Words = new String[4]; 
        Words[0] = "Android";
        Words[1] = "Java";
        Words[2] = "CSS";
        Words[3] = "JavaScript";   

        for(String word : Words) {
            System.out.println(word); 
        } 
     }
}
You can try this sample in your own IDE.
   
Comments
four systems 25-Aug-19 8:20am
   
thancs

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100