The Artima Developer Community
Sponsored Link

Java Answers Forum
A question about Java array

2 replies on 1 page. Most recent reply: Feb 5, 2011 6:28 AM by Charles Bell

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 2 replies on 1 page
Baran Dorell

Posts: 1
Nickname: barand
Registered: Jan, 2011

A question about Java array Posted: Jan 1, 2011 5:38 AM
Reply to this message Reply
Advertisement
 public class PlaneSeatMain
{
  public static void main(String[] args)
  {
    char[][] seat = 
    {  
      {'X', '*', '*', '*', '*', '*'}, 
      {'*', 'X', '*', '*', '*', '*'},
      {'*', 'X', '*', '*', '*', '*'},
      {'*', '*', '*', '*', '*', '*'}, 
      {'*', '*', '*', '*', '*', '*'}, 
      {'*', '*', '*', '*', '*', '*'}, 
      {'*', '*', 'X', '*', '*', '*'},
      {'*', '*', '*', '*', '*', '*'}, 
      {'*', '*', '*', 'X', '*', '*'}, 
      {'*', '*', '*', '*', '*', '*'}, 
      {'*', '*', '*', '*', '*', '*'}, 
      {'*', '*', '*', '*', '*', '*'}, 
      {'*', '*', '*', '*', '*', '*'}, 
    };
    
    System.out.println("seat 0,0: " + seat[0][0]);
    System.out.println("seat 2,4: " + seat[2][4]);
    
  }
} 


I want to change values of seat as:


 char[][] seat = 
    {  
      {'X', '*', '*', '*', '*', '*'}, 
      {'*', 'X', '*', '*', '*', '*'},
      {'*', 'X', '*', '*', '*', '*'},
      {'*', '*', '*', '*', '*', '*'}, 
      {'*', '*', '*', '*', '*', '*'}, 
      {'*', '*', '*', '*', '*', '*'}, 
      {'*', '*', 'X', '*', '*', '*'},
      {'*', '*', '*', '*', '*', '*'}, 
      {'*', '*', '*', 'X', '*', '*'}, 
      {'*', '*', '*', '*', '*', '*'}, 
      {'*', '*', '*', '*', '*', '*'}, 
      {'*', '*', '*', '*', '*', '*'}, 
      {'*', '*', '*', '*', '*', '*'}, 
    }; 


but after using

 char[][] seat =
{
...
}; 


the compiler gives error if I use it again to change the values, because:

"seat is already defined in main(java.lang.String[])"


Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Re: A question about Java array Posted: Jan 2, 2011 5:48 AM
Reply to this message Reply
You only need to use "char[][]" once, to define seat as a char array. Thereafter, just use "seat =" to update its value.

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: A question about Java array Posted: Feb 5, 2011 6:28 AM
Reply to this message Reply
     seat = new char[][]    {  
      {'X', '*', '*', '*', '*', '*'}, 
      {'*', 'X', '*', '*', '*', '*'},
      {'*', 'X', '*', '*', '*', '*'},
      {'*', '*', '*', '*', '*', '*'}, 
      {'*', '*', '*', '*', '*', '*'}, 
      {'*', '*', '*', '*', '*', '*'}, 
      {'*', '*', 'X', '*', '*', '*'},
      {'*', '*', '*', '*', '*', '*'}, 
      {'*', '*', '*', 'X', '*', '*'}, 
      {'*', '*', '*', '*', '*', '*'}, 
      {'*', '*', '*', '*', '*', '*'}, 
      {'*', '*', '*', '*', '*', '*'}, 
      {'*', '*', '*', '*', '*', '*'}, 
    }; 
 

Flat View: This topic has 2 replies on 1 page
Topic: a little challege Previous Topic   Next Topic Topic: java

Sponsored Links



Google
  Web Artima.com   

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