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.

Page 1 of 2 12 LastLast
Results 1 to 25 of 37

Thread: Array related assignment

  1. #1
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Post Array related assignment

    Hi guys and girls, nice to post again. Once again I have a problem, this time I have to create a.. welll here is what is says

    "Create a program that uses a multidimensional array to store student grades. The first dimension should be a number for each student, and the second dimension should be for each studentís grades. Display the average of all the grades earned by each student and an overall average for every student.".

    My problem? How do I create a multidimensional array and store something in one and something in the other dimension? Please don't give full code, I don't the whole answer just some help.


    THanks!!

    Tryin and Readin and surely some Gamin!!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,424
    Thanks
    49
    Thanked 2,215 Times in 2,188 Posts

    Default Re: Array related assignment

    How do I create a multidimensional array
    int twoDim[][] = new int[5][]; // create a two dim array with second dim unassigned

    Then to add second dim:
    twoDim[0] = new int[4];

    To put data in:
    twoDim[0][0] = 123;

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Array related assignment

    If you want a multi-dimensional arraylist, see the tips and tricks section (you might have to do a search for it). The basic premise is this, though: A multi-dimensional array is just an array of arrays of arrays of... etc. etc.

    So, to declare a 2-d array, just put two square brackets right next to each other.
    int[][] my2dArray;

    To initalize it, you have two choices in Java. As I said before, it is basically an array of arrays, so you initialize the main array and then initialize each sub-array and put their reference into the array of arrays. With this method, you can create "jagged arrays", or those that have rows with different numbers of elements.

    int[][] my2dArray = new int[5][];
    for(int i = 1; i <= my2dArray.length; i++)
    {
         // initialize an int array and put it into my2dArray
         my2dArray[i-1] = new int[i];
    }

    Alternatively, you can provide all the indices at the beginning and you can create a non-jagged array.

    int[][] my2dArray = new int[5][3]; // creates a 5x3 array

  4. #4
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: Array related assignment

    First sorry i forgot to include this link Programming with Java in 24 Hours: Storing Information with Arrays
    You can see the full code there, its the one called StudentGrader.java.
    And cloud you guys make it a little more clearer please.

    Thanks
    _________________________________________
    Tryin and Readin and surely some Gamin!!

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,424
    Thanks
    49
    Thanked 2,215 Times in 2,188 Posts

    Default Re: Array related assignment

    Try writing a small program using the above examples. Make some changes, set one of the elements, display some of the elements.
    Come back when you have problems. Post the code and the errors.

  6. #6
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: Array related assignment

    I tried but i couldn't do any thing cause I didn't understand the examples.

    _________________________________________
    Tryin and Readin and surely some Gamin!!

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,424
    Thanks
    49
    Thanked 2,215 Times in 2,188 Posts

    Default Re: Array related assignment

    Try copy and paste of any one of the above into a main() method.
    Are you able to write a "Hello World" program?
    Take that program and insert into it one of the above.

  8. #8
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Unhappy Re: Array related assignment

    I tried them and here is what I got, I started by the one I understood completely
    Quote Originally Posted by helloworld922;12746

    [code
    int[][] my2dArray = new int[5][3]; // creates a 5x3 array[/code]
    My code:
    class Test {
        public static void main(String[] args) {
            int[][] my2dArray = new int[5][3];
            int num = 33;
            my2dArray[2][2] = 3 * 33;
            System.out.println(my2dArray[2][2]);
        }
    }
    My result:
    run:
    99

    Second one
    Quote Originally Posted by helloworld922;12746

    int [
    [ ] my2dArray = new int[5][];
    for(int i = 1; i <= my2dArray.length; i++)
    {
    // initialize an int array and put it into my2dArray
    my2dArray[i-1] = new int[i];
    }
    My code:
    class Test2 {
        public static void main(String[] args) {
            int[][] my2dArray = new int[5][];
        for(int i = 1; i <= my2dArray.length; i++){
         // initialize an int array and put it into my2dArray
         my2dArray[i-1] = new int[i];
         System.out.print(my2dArray[4][0]);
             }
        }
    }

    My result:
    Exception in thread "main" java.lang.NullPointerException
    at Test2.main(Test2.java:7)
    Java Result: 1

    Line 7 :
         System.out.print(my2dArray[4][0]);

    Have no clue whats wrong, please make it clear. No errors at all till Netbeans runs it.

  9. #9
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Array related assignment

    you're trying to print out information before it's initialized. Move the print statement outside the for loop
    for(int i = 1; i <= my2dArray.length; i++)
    {
        my2dArray[i-1] = new int[i];
    }
    System.out.println(my2dArray[4][0]);

  10. #10
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: Array related assignment

    Here is what i got:
    run:
    0

    But I still don't understand what is happening in the for loop.

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,424
    Thanks
    49
    Thanked 2,215 Times in 2,188 Posts

    Default Re: Array related assignment

    Each dimension of an array must be initialized separately.
    int[][] twoDim = new int[5][]; // define a two dim array
    So now twoDim has 5 empty slots where there needs to be added one dim arrays.
    So the code goes thru a loop 5 times to add that one dim array to twoDim
    twoDim[i] = new int[4]; // add the second dim array

    At the end of the loop twoDim has 5 elements, each of which is one dim array of 4 elements.

    Using indexes you can go from element twoDim[0][0] to twoDim[4][3]

  12. #12
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: Array related assignment

    Can you tell me what the I in
    twoDim[i] = new int[4];
    stands for?

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,424
    Thanks
    49
    Thanked 2,215 Times in 2,188 Posts

    Default Re: Array related assignment

    I don't see an I in what you posted. I do see an i. Case is important in Java i and I are different.

    The variable i in twoDim[i] is an index into the twoDim array. The statement
    twoDim[i] = new int[4];
    would create a 4 element array and put its address into the twoDim array at element i. i could have a value from 0 to 4 because twoDim[] has five elements

  14. #14
    Member Faz's Avatar
    Join Date
    Mar 2010
    Posts
    97
    Thanks
    5
    Thanked 14 Times in 14 Posts

    Default Re: Array related assignment

    Norm are you sure about the way you initialise an array I haven't worked with them for a while and never have extensively but I was under the impression they could be initialised this way:

    int[][] twoDim = new int[5][4];

  15. #15
    Member Faz's Avatar
    Join Date
    Mar 2010
    Posts
    97
    Thanks
    5
    Thanked 14 Times in 14 Posts

    Default Re: Array related assignment

    Quote Originally Posted by Melawe View Post
    Here is what i got:
    run:
    0

    But I still don't understand what is happening in the for loop.
    It comes up as 0 because you haven't declared it yet. It came up with an error because you were trying to show a value that hadn't been initialised yet(only the first element has been initialised after the first run through.

    I assume you are using the loop in helloworld's post
    for(int i = 1; i <= my2dArray.length; i++)
    {
        my2dArray[i-1] = new int[i];
    }
    System.out.println(my2dArray[4][0]);

    whats happening is creating an array that looks like

    #
    ##
    ###
    ####
    #####

    where # represents a space in the array if that makes sense

    if you said
    int[][] my2dArray = new int[5][5];

    on the other hand it would look like this(I say look but maybe that's not the best way of putting it but I find it's good to visualise it)

    #####
    #####
    #####
    #####
    #####

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,424
    Thanks
    49
    Thanked 2,215 Times in 2,188 Posts

    Default Re: Array related assignment

    I try to write a short program to test before posting. The following compiles and executes.
    public class TestArrayDef {
     
      public static void main(String[] args) {
         int twoDim[][] = new int[5][];
         twoDim[0] = new int[2];
         twoDim[0][0] = 123;
         System.out.println("twoDim[0][0] =" + twoDim[0][0]);
     
         int[][] my2dArray = new int[5][5];
      }
    }
    Running: D:\Java\jre6_10\bin\java.exe -classpath D:\JavaDevelopment;. -Xmx512M TestArrayDef

    twoDim[0][0] =123

    0 error(s)

  17. #17
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: Array related assignment

    Okay thanks everyone!! Cloud you guys help me with one more thing please, storing data in one dim and storing some other data in the second dim.

    Thanks!

  18. #18
    Member Faz's Avatar
    Join Date
    Mar 2010
    Posts
    97
    Thanks
    5
    Thanked 14 Times in 14 Posts

    Default Re: Array related assignment

    Well looking at your original post you say:

    Quote Originally Posted by Melawe View Post
    "Create a program that uses a multidimensional array to store student grades. The first dimension should be a number for each student, and the second dimension should be for each student’s grades. Display the average of all the grades earned by each student and an overall average for every student.".
    So from my understanding you just need a 2D array of two "columns" so it would look something like this

    1 65
    2 50
    3 80
    4 55

    Where the first column is the student number and the second is the grade. Am I correct in this assumption? If so you just need a 2D array of ints right?

    Although this part has me a bit in doubt "Display the average of all the grades earned by each student and an overall average for every student"

  19. #19
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: Array related assignment

    Yep thats what it says.

  20. #20
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    471
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Array related assignment

    Quote Originally Posted by helloworld922 View Post
    To initalize it, you have two three choices in Java.
    Shouldn't that be 'three choices'?

    Don't forget array initializers:
    int twoDim[][] = { 
       { 1, 2, 3 }, 
       { 4, 5, 6 } 
    };

  21. #21
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Lightbulb Re: Array related assignment

    Sorry guys I didn't reply in a while. I'm trying some things out, nothing worked yet but here is what I was doing and what I got. I thought it would be easier to do if I knew how to do it in one dim arrays, then all i had to do was squeeze them to in a two dim array.
    class NewAry {
        public static void main(String[] args){
            int[] stud = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
            int[]grd= new int[14];
            int grd[0]= 60;
            System.out.println(int grd[0]);
        }
    }
    DLorde could you tell me what the lines of code in your post will do and how I can modify it to suit my needs more?
    Thanks!
    Quote Originally Posted by dlorde View Post
    Shouldn't that be 'three choices'?

    Don't forget array initializers:
    int twoDim[][] = { 
       { 1, 2, 3 }, 
       { 4, 5, 6 } 
    };

  22. #22
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: Array related assignment

    When I tried it out i got this result:

    run:
    [[I@3e25a5

    class JForumtest{
        public static void main(String[] args){
            int twoDim[][] = {
                { 1, 2, 3 },
                { 4, 5, 6 } };
            System.out.println(twoDim);
        }
    }
    Excuse "t" in the file name.

  23. #23
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,424
    Thanks
    49
    Thanked 2,215 Times in 2,188 Posts

    Default Re: Array related assignment

    [[I@3e25a5
    That's what the JVM will give you when you ask for a String representation of an object that does not have a toString() method to convert it for human viewing. Arrays are objects. The String representation for twoDim: [[I says two dimensions of int
    and the @3e25a5 gives the object's location in memory. Not very useful info for you, but sometimes it is when debugging.

    Their are some methods that you can pass an array reference to and it will generate nicer looking output. Look at the Arrays class.

  24. #24
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: Array related assignment

    Could you tell me whats wrong in this

    class NewAry {
        public static void main(String[] args){
            int[] stud = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
            int[]grd= new int[14];
            int grd[0]= 60;
            System.out.println(int grd[0]);
        }
    }
    And what the error message is when I run the NewAry program(code is in first post page 3)?

  25. #25
    Member Faz's Avatar
    Join Date
    Mar 2010
    Posts
    97
    Thanks
    5
    Thanked 14 Times in 14 Posts

    Default Re: Array related assignment

    Drop the int from the println statement like so
           System.out.println(grd[0]);

    EDIT:
    And the line before that you only need to declare the type of the array once.

    grd[0]= 60;

Page 1 of 2 12 LastLast

Similar Threads

  1. Funny computer and office related cartoons
    By Flash in forum The Cafe
    Replies: 6
    Last Post: April 9th, 2010, 04:13 AM
  2. What is SNMP in Java?Hoe to execute code related to this?
    By jj_crazy_1 in forum Java Theory & Questions
    Replies: 0
    Last Post: April 5th, 2010, 10:47 PM
  3. Replies: 1
    Last Post: February 22nd, 2010, 07:20 AM
  4. Related to Html and Java Script?
    By JackyRock in forum JavaServer Pages: JSP & JSTL
    Replies: 2
    Last Post: February 17th, 2010, 02:10 AM
  5. Swing problem, newbie'ish . Perhaps thread related?
    By fenderman in forum AWT / Java Swing
    Replies: 3
    Last Post: July 22nd, 2009, 04:45 AM