Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: General 2D Array Question

  1. #1
    Junior Member
    Join Date
    Jul 2022
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default General 2D Array Question

    Extremely basic array question as I've seen it done both ways and I want to know the general consensus on what is the proper way as I have a project I've had to complete and realized I've seen a few snippets of code perform the loading of array data in a weird way and I'm just wondering if it is my lack of knowledge that thinks it is weird.

    An array can be loaded either vertically or horizontally depending on how the objects are entered. With the two types of string arrays creating two different types of structures.

    With capitals below creating a more vertical structure that would create essentially 2 columns with 50 rows and capitalCities creating 50 columns with 2 rows.

    The novice in me believes that the 1st version of this with the city pairs in the vertical version of this is the accurate way to do this, but I'm honestly not sure what is the preferred method in the programming world. As I mentioned, I'm very much a beginner, but noticed the difference in these two items and just want to know what the norm is as I can see the different, but don't know the practical, real world application of either approach.

    Thanks.



    	String[][] capitals ={
                {"Alabama", "Montgomery"},{"Alaska", "Juneau"},{"Arizona", "Phoenix"},{"Arkansas", "Little Rock"}, {"California", "Sacramento"},
                {"Colorado", "Denver"}, {"Connecticut", "Hartford"}, {"Delaware", "Dover"}, {"Florida", "Tallahassee"},{"Georgia", "Atlanta"},
                {"Hawaii", "Honolulu"},{"Idaho", "Boise"},{"Illinois", "Springfield"},{"Maryland", "Annapolis"},{"Minnesota", "Saint Paul"},  {"Iowa", "Des Moines"},
                {"Maine", "Augusta"}, {"Kentucky", "Frankfort"},  {"Indiana", "Indianapolis"},{"Kansas", "Topeka"},{"Louisiana", "Baton Rouge"},{"Oregon", "Salem"},
                {"Oklahoma", "Oklahoma City"},{"Ohio", "Columbus"},{"North Dakota", "Bismark"},{"New York", "Albany"},{"New Mexico", "Santa Fe"},{"New Jersey", "Trenton"},
                {"New Hampshire", "Concord"},{"Nevada", "Carson City"},{"Nebraska", "Lincoln"},{"Montana", "Helena"},{"North Carolina", "Raleigh"},{"Missouri", "Jefferson City"},
                {"Mississippi", "Jackson"}, {"Massachusetts", "Boston"}, {"Michigan", "Lansing"},{"Pennsylvania", "Harrisburg"},{"Rhode Island", "Providence"},
                {"South Carolina", "Columbia"},{"South Dakota", "Pierre"}, {"Tennessee", "Nashville"},{"Texas", "Austin"},{"Utah", "Salt Lake City"},
                {"Vermont", "Montpelier"},{"Virginia", "Richmond"},{"Washington", "Olympia"},{"West Virginia", "Charleston"},{"Wisconsin", "Madison"}, {"Wyoming", "Cheyenne"} 
        };

    String[][] capitalCities = {
                {"Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut", "Delaware", "Florida", "Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico", "New York", "North Carolina", "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington", "West Virginia", "Wisconsin", "Wyoming"},
                {"Montgomery", "Juneau", "Phoenix", "Little Rock", "Sacramento", "Denver", "Hartford", "Dover", "Tallahassee", "Atlanta", "Honolulu", "Boise", "Springfield", "Indianapolis", "Des Moines", "Topeka", "Frankfort", "Baton Rouge", "Augusta", "Annapolis", "Boston", "Lansing", "Saint Paul", "Jackson", "Jefferson City", "Helena", "Lincoln", "Carson City", "Concord", "Trenton", "Santa Fe", "Albany", "Raleigh", "Bismarck", "Columbus", "Oklahoma City", "Salem", "Harrisburg", "Providence", "Columbia", "Pierre", "Nashville", "Austin", "Salt Lake City", "Montpelier", "Richmond", "Olympia", "Charleston", "Madison", "Cheyenne"}
        };

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: General 2D Array Question

    Personally I prefer the 50 rows with 2 columns. It is easier to visually verify the data since the state and capital are together.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Very general question about what I can do with Java.
    By DaftVader in forum Java Theory & Questions
    Replies: 0
    Last Post: October 24th, 2021, 08:41 AM
  2. [SOLVED] General question on using array inside a nested for loop
    By CADman in forum What's Wrong With My Code?
    Replies: 10
    Last Post: March 30th, 2021, 04:23 PM
  3. A general question about online help
    By michael.duffy31 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 9th, 2013, 01:27 AM
  4. General Question; Need Ideas
    By clydefrog in forum Java Theory & Questions
    Replies: 2
    Last Post: March 28th, 2012, 04:42 PM
  5. General Question
    By Becca in forum Collections and Generics
    Replies: 6
    Last Post: November 3rd, 2011, 03:52 PM