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 2 of 2 FirstFirst 12
Results 26 to 37 of 37

Thread: Array related assignment

  1. #26
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Array related assignment

    tell me whats wrong in this
    What error message did the compiler give you for the error? There could be an explanation there.

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

    Default Re: Array related assignment

    Thanks Faz I got it with no errors! One thing though, how can I make a single array element equal to more than one number this wont work
    grd[0] = 60,75,93;
     
    //or
     
    grd[0] = {60,75,93};
    and not sure what will...

    (thanks for pointing out the "it" Norm )
    Last edited by Melawe; June 14th, 2010 at 04:50 PM.

  3. #28
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Array related assignment

    how can I make it equal to more than one number
    What is the it in the above?
    A variable or an element of an array can only hold one value at a time. You can change the values one after the other so that it has held all the values. But at any instance of time, it will only hold one value.

    To initialize an array of ints you can code: int[] ints = {1,2,3}; // create an array with 3 elements and put values into each

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

    Default Re: Array related assignment

    Quote Originally Posted by Melawe View Post
    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?
    The code I posted initialises a 2D array with contents, creating an array of two arrays of 3 ints. The outer curly braces define the bounds of the outer array. Inside these, each pair of inner curly braces defines an element of the outer array. Inside the inner pairs of curly braces, the elements of the inner arrays are defined.

    I don't know what your needs are, so I can't tell you how to modify it to suit them. Once you understand the code, you should be able to do it yourself.

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

    Default Re: Array related assignment

    Hiya guys, Thanks for your help! But I can't seem to figure it out, i'm gonna head to the books site and see the code later(if I don't get posts telling me to "wait for a sec!!" :/).
    Last edited by Melawe; June 19th, 2010 at 03:10 AM.

  6. #31
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Array related assignment

    my2dArray[i-1] = new int[i];
    The assignment statement in the for loop is assigning a value to an element (at i-1) of the array my2dArray.
    The value being assigned is another array of dimension i.

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

    Default Re: Array related assignment

    Quote Originally Posted by dlorde View Post
    Don't forget array initializers:
    int twoDim[][] = { 
       { 1, 2, 3 }, 
       { 4, 5, 6 } 
    };

    Quote Originally Posted by dlorde View Post
    The code I posted initialises a 2D array with contents, creating an array of two arrays of 3 ints. The outer curly braces define the bounds of the outer array. Inside these, each pair of inner curly braces defines an element of the outer array. Inside the inner pairs of curly braces, the elements of the inner arrays are defined.

    I don't know what your needs are, so I can't tell you how to modify it to suit them. Once you understand the code, you should be able to do it yourself.

    I finally got your code!!!

    [code]class JForumtest{
    public static void main(String[] args){
    int twoDim[][] = {
    { 1, 2, 3 },
    { 4, 5, 6 } };
    System.out.println(twoDim[1][2]);
    }
    }
    [\code]

    Result:
    run:
    6
    Last edited by Melawe; June 19th, 2010 at 08:06 PM.

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

    Question Re: Array related assignment

    I tried a few more times but I just couldn't figure it out, I finally directed my browser to the site today and here is the code
    class StudentGrades {
            public static void main(String arguments[]) {
    		int[][] student = new int[6][6];
                    int[] janice = { 90,85,85,75,70,95 };
                    int[] mikey = { 95,85,85,70,75,90 };
                    int[] nicki = { 85,80,85,95,100,100 };
                    int[] bernice = { 80,90,90,80,85,95 };
                    int[] ernest = { 75,45,80,95,90,90 };
                    int[] bigMike = { 90,90,90,95,70,65 };
                    student[0] = janice;
                    student[1] = mikey;
                    student[2] = nicki;
                    student[3] = bernice;
                    student[4] = ernest;
                    student[5] = bigMike;
                    int gradeSum = 0;
                    int[] studentSum = new int[6];
                    // Loop through the students
                    for (int i = 0; i < 6; i++) {
                            System.out.println("Student #" + i);
                            studentSum[i] = 0;
                            // Loop through each student's grades
                            for (int j = 0; j < 6; j++) {
                                    gradeSum = gradeSum + student[i][j];
                                    studentSum[i] = studentSum[i] + student[i][j];
                            }
                            System.out.println("  Average grade: "
                                    + studentSum[i] / 6);
                    }
                    System.out.println("\nAverage grade of all students: " +
                            gradeSum / 36);
    	}
    }
    run:
    Student #0
    Average grade: 83
    Student #1
    Average grade: 83
    Student #2
    Average grade: 90
    Student #3
    Average grade: 86
    Student #4
    Average grade: 79
    Student #5
    Average grade: 83

    Average grade of all students: 84


    and here is the code I wrote
    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[] alem = {60,70,85};
            int[] jon = {99,23,58};
            int[] yohanes = {86,33,67};
            int[] hagos = {35,26,98};
            int[] grade = new int [3];
            grade[2] = hagos[2];
     
            System.out.println(grade[2]);
        }
    }
    run:
    98


    One other problem I had is I didn't know you can do this
    int[][] student = new int[6][6];
    student[0] = janice;

    with a two dim array. But when I tried it
    int[][] test = new int [4][4];
    test[3] = hagos;
    got this output : [I@3e25a5

    I'm not sure why I got this out put [I@3e25a5 while it worked fine in the code from the site...
    And thats what I need help with right now why I got that funny output?


    Thanks every one for your help!!! Here's a Thank you! cup of coffee to every one!

  9. #34
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Array related assignment

    I don't see where your println() is that generated the output you show.
    [I@3e25a5 is what is generated by the toString() method for the array object. Arrays are objects. The Object class has a toString() method that returns a String with data about the object.
    The [ indicates an array, the I indicates integer, the @3e25a5 is the address in memory.

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

    Default Re: Array related assignment

    I'm having a little trouble understanding the for loop in the StudentGrader class, can you make it a little clearer please?

    class StudentGrades {
            public static void main(String arguments[]) {
    		int[][] student = new int[6][6];
                    int[] janice = { 90,85,85,75,70,95 };
                    int[] mikey = { 95,85,85,70,75,90 };
                    int[] nicki = { 85,80,85,95,100,100 };
                    int[] bernice = { 80,90,90,80,85,95 };
                    int[] ernest = { 75,45,80,95,90,90 };
                    int[] bigMike = { 90,90,90,95,70,65 };
                    student[0] = janice;
                    student[1] = mikey;
                    student[2] = nicki;
                    student[3] = bernice;
                    student[4] = ernest;
                    student[5] = bigMike;
                    int gradeSum = 0;
                    int[] studentSum = new int[6];
                    // Loop through the students
                    for (int i = 0; i < 6; i++) {
                            System.out.println("Student #" + i);
                            studentSum[i] = 0;
                            // Loop through each student's grades
                            for (int j = 0; j < 6; j++) {
                                    gradeSum = gradeSum + student[i][j];
                                    studentSum[i] = studentSum[i] + student[i][j];
                            }
                            System.out.println("  Average grade: "
                                    + studentSum[i] / 6);
                    }
                    System.out.println("\nAverage grade of all students: " +
                            gradeSum / 36);
    	}
    }

  11. #36
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Array related assignment

    The 2 dim array holds student data. The i index is for the student and the j index is for the scores for that student.

    studentSum[i] = studentSum[i] + student[i][j];
    or
    studentSum[i] += student[i][j];
    Its incrementing the sum for student i by all of his scores indexed by j

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

    Default Re: Array related assignment

    Thanks guys for all your help!! I'm marking this Solved!

    Thanks again!

Page 2 of 2 FirstFirst 12

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, 08:20 AM
  4. Related to Html and Java Script?
    By JackyRock in forum JavaServer Pages: JSP & JSTL
    Replies: 2
    Last Post: February 17th, 2010, 03: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