Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
1.00/5 (1 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

The reason you are getting 4 outputs is because you have 4 elements in the array,
Java
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,
Java
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,
Java
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.
 
Share this answer
 
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, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900