Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java confusion with arrays

Options
  • 25-02-2013 7:56pm
    #1
    Registered Users Posts: 1,551 ✭✭✭


    I was trying to make a java video game shop application just to help me learn.
    I'm a bit confused now though.
    I'm trying to define my Game class to belong to 1 or more systems so I was thinking of using a String array but I'm getting a bit lost trying to get it working when you have a String array.
    I can post up some of my code so far and you might see how confused I am.
    public class Game
    {
    	private String title;
    	private String[] system = new String[1000];
    	private double price;
    	private int copies;
    	private String genre;
    	public boolean isAvailable;
    	
    	public Game()
    	{
    		this.title = "";
    		this.system = new String[0];
    		this.price = 0.00;
    		this.copies = 0;
    		this.genre = "";
    		this.isAvailable = false;
    		
    	}
    	public Game(String title, String[] system, double price, int copies, String genre,boolean isAvailable )
    	{
    		this.title = title;
    		this.system = system;
    		this.price = price;
    		this.copies = copies;
    		this.genre = genre;
    		this.isAvailable = isAvailable;
    		
    		
    	}
    	
    	public String getTitle()
    	{
    		return title;
    	}
    	
    	public void setTitle(String title)
    	{
    		this.title= title;
    	}
    	
    	public String[] getSystem()
    	{
    		return system;
    	}
    	
    	public void setSystem(String[] system)
    	{
    		this.system= system;
    	}
    	
    
    
    
    

    Because its a string array I'm not really sure how to use "this" with it and how to initialise it.
    Please help.


Comments

  • Registered Users Posts: 51 ✭✭GavinFlud


    You don't need the "new String[1000]" since once the Game object is created the variable will either be initialized to an empty array or to one passed to the constructor. Outside of that, this should work once the Game object is created.


  • Registered Users Posts: 2,089 ✭✭✭henryporter


    Exactly; just declare the array like any other variable:

    String [] system;

    And leave it at that.


  • Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,092 Mod ✭✭✭✭Tar.Aldarion


    Do you want to make an array?
    this.system = new String[0];
    

    or do you want to say
    this.system = system;
    

    Of course what you have is fine and probably what you want, just in case.
    Actually I'm talking nonsense, time to sleep. Their suggestion is the only one you need


  • Registered Users Posts: 1,551 ✭✭✭quinnd6


    Because its an array the only way I can actually return any of the values is using a foreach loop.
    Is there any neater way of doing it than what I'm doing here to return the elements of the array of systems?
    This is my new code.
    public class Game
    {
    	private String title;
    	private String[] system;
    	private double price;
    	private int copies;
    	private String genre;
    	public boolean isAvailable;
    	
    	public Game()
    	{
    		this.title = "";
    		this.system = new String[0];
    		this.price = 0.00;
    		this.copies = 0;
    		this.genre = "";
    		this.isAvailable = false;
    		
    	}
    	public Game(String title, String[] system, double price, int copies, String genre,boolean isAvailable )
    	{
    		this.title = title;
    		this.system = system;
    		this.price = price;
    		this.copies = copies;
    		this.genre = genre;
    		this.isAvailable = isAvailable;
    		
    		
    	}
    	
    	public String getTitle()
    	{
    		return title;
    	}
    	
    	public void setTitle(String title)
    	{
    		this.title= title;
    	}
    	
    	public String[] getSystem()
    	{
    		
    			return system;
    		
    	}
    	
    	
    	
    	public void setSystem(String[] system)
    	{
    		this.system= system;
    	}
    	
    }
    
    public class GameTest
    {
    		public static void main(String args[])
    		{
    				
    				Game g = new Game("Crysis", new String[]{"pc"},10.00, 20, "FPS",true);
    				
    				g.setPrice(5.00);
    				for(String s:g.getSystem())
    				{
    					System.out.println(g.getTitle()+ " "+g.getPrice() +" "+s);
    				}
    				
    				g.setSystem(new String[]{"PC","PS3"});
    				for(String s:g.getSystem())
    				{
    					System.out.println(g.getTitle()+ " "+g.getPrice() +" "+s);
    				}
    				//System.out.println(g.getTitle()+ " "+g.getPrice()+ " "+s);
    		}
    }
    
    
    


  • Registered Users Posts: 51 ✭✭GavinFlud


    I'm afraid not, in order to gain access to each value in an array, you're going to have to loop through it. To be fair, it's quite a neat way of doing it since you have access to a specific element of the array inside the for loop. When you start to use Collections there is a nice object called an iterator that enables you to cycle through a them. Again though, it's all done using loops.


  • Advertisement
  • Registered Users Posts: 1,551 ✭✭✭quinnd6


    Good stuff thanks.


  • Registered Users Posts: 51 ✭✭GavinFlud


    quinnd6 wrote: »
    Because its an array the only way I can actually return any of the values is using a foreach loop.
    Is there any neater way of doing it than what I'm doing here to return the elements of the array of systems?

    Just reading back over your comment, I may not have given a comprehensive response. If you want to do something with each value in an array, just do:
    for (String s : systems) {
      System.out.println(s);
    }
    
    or:
    for (int i = 0; i < systems.length; i++) {
      System.out.println(systems[i]);
    }
    
    or if you only want access to certain values in the array:
    System.out.println(systems[3]);
    

    Those are pretty much the only (and therefore, neatest) ways to use arrays.


  • Registered Users Posts: 8,981 ✭✭✭Tim Robbins


    Always go for this approach.
    for (String s : systems) {
      System.out.println(s);
    }
    
    or:

    over this
    for (int i = 0; i < systems.length; i++) {
      System.out.println(systems[i]);
    }
    

    The latter suffers for the fallacies of imperative programming while the former benefits from some of the ideas in functional programming where there is a separation between the what? and how?


  • Registered Users Posts: 51 ✭✭GavinFlud


    Always go for this approach.
    for (String s : systems) {
      System.out.println(s);
    }
    
    or:

    over this
    for (int i = 0; i < systems.length; i++) {
      System.out.println(systems[i]);
    }
    
    The latter suffers for the fallacies of imperative programming while the former benefits from some of the ideas in functional programming where there is a separation between the what? and how?

    While I'd always advocate using the for-each loop whenever possible, there are times when you need access to the index and the simple option is to use the original for loop.


  • Registered Users Posts: 8,981 ✭✭✭Tim Robbins


    GavinFlud wrote: »
    While I'd always advocate using the for-each loop whenever possible, there are times when you need access to the index and the simple option is to use the original for loop.
    Can you give an example?


  • Advertisement
  • Registered Users Posts: 2,021 ✭✭✭ChRoMe


    Can you give an example?

    Can be handy if you only want to deal with every nth element or whatever.


  • Registered Users Posts: 8,981 ✭✭✭Tim Robbins


    ChRoMe wrote: »
    Can be handy if you only want to deal with every nth element or whatever.

    Not saying you are wrong. But why would you want to deal with n'th element as opposed to something that is more business related?


  • Registered Users Posts: 51 ✭✭GavinFlud


    Not saying you are wrong. But why would you want to deal with n'th element as opposed to something that is more business related?

    Not only that, say you have an array of objects and an array of several indexes and you need to remove each object from the first array that has an index equal to an index in the second array. The easiest way to do that would be to loop through the array and perform a check on each iteration.

    Again, 90% of the time I would use the for-each but there are times when the original for loop is useful.


  • Registered Users Posts: 8,981 ✭✭✭Tim Robbins


    GavinFlud wrote: »
    Not only that, say you have an array of objects and an array of several indexes and you need to remove each object from the first array that has an index equal to an index in the second array. The easiest way to do that would be to loop through the array and perform a check on each iteration.

    Again, 90% of the time I would use the for-each but there are times when the original for loop is useful.

    But again that's not business related.

    What I am getting at here is that there should be a separation between the what and the how. The what might be remove all cars entities from the collection of cars where the cars have the steering wheel on the wrong side.

    So while waiting for Java 8 where you use filter functions for this you could use Google's Collections: https://code.google.com/p/guava-libraries/

    Now you could say ah sure it is easy to write you imperative approach for this. It is but it also a hot spot for bugs.


  • Registered Users Posts: 51 ✭✭GavinFlud


    But again that's not business related.

    What I am getting at here is that there should be a separation between the what and the how. The what might be remove all cars entities from the collection of cars where the cars have the steering wheel on the wrong side.

    So while waiting for Java 8 where you use filter functions for this you could use Google's Collections: https://code.google.com/p/guava-libraries/

    Now you could say ah sure it is easy to write you imperative approach for this. It is but it also a hot spot for bugs.

    I understand where you're coming from and clean separation of the 'what' and the 'how' is a critical design choice when working on mid to large sized applications. However, with regards to the OP and the question he asked (which implies he's a relative beginner to programming), there are definitely going to be times when he'll need to use a standard for-loop for the small projects he'll be working on.


Advertisement