0

Very new to Java and programming in general. trying to figure out how to print the contents of my array

public class GameClass {
    public static void main(String args[]) {
        String [] gameList = new String [] {"Call of Duty", "Skyrim", "Overwatch", "GTA", "Castlevania", "Resident Evil", "HALO", "Battlefield", "Gears of War", "Fallout"};
        System.out.println(gameList[]);
    }
}
Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • 1
    Note that this is an array, not arraylist. Arrays are created `new Type[size]` while lists such as ArrayList via `new ArrayList()`. Arrays have fixed size while lists are resizable. – Pshemo Mar 15 '20 at 14:54

4 Answers4

1

You can use the util package for one line printing

System.out.println(java.util.Arrays.toString(gameList));
Rans
  • 569
  • 7
  • 14
0

Should do the trick.

 for(int i = 0 ; i <gameList.length; i ++)
     System.out.print(gameList[i]+" ");
RRIL97
  • 858
  • 4
  • 9
0

Just iterate each element and print:

for(String s : gameList){
    System.out.println(s);
}
Pshemo
  • 122,468
  • 25
  • 185
  • 269
hugoalexandremf
  • 166
  • 1
  • 1
  • 9
0

You can print out your array of games by using the following loop:

for(String game : gameList){
     System.out.println(game);
}

This runs through each item in your gameList array and prints it.

Pshemo
  • 122,468
  • 25
  • 185
  • 269
Seiont
  • 59
  • 8