Results 1 to 5 of 5
  1. #1
    tnahsin is offline Member
    Join Date
    Mar 2014
    Posts
    8
    Rep Power
    0

    Default how to give vlaues to array objects without using loop?

    The main method should creates a Student object with name as “Bill” and marks as {88,92,76,81,83} and print it.

    Java Code:
    class TestStudent {
        public static void main(String args[]) {
            Student1 st = new Student1();
            st.name = "bill";
            Student st1[] = new Student[6];
            //This gives error
            st1[].marks = {1, 5, 8, 9, 7, 6};

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,994
    Rep Power
    5

    Default Re: how to give vlaues to array objects without using loop?

    Well you are not providing an index to the st1 array, so no wonder you are getting an error.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    tnahsin is offline Member
    Join Date
    Mar 2014
    Posts
    8
    Rep Power
    0

    Default Re: how to give vlaues to array objects without using loop?

    do i have to put st1[0].marks=1 ,st1[1].marks=2 etc till st1[5]. Is there any other way i can pass the complete array values together

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,994
    Rep Power
    5

    Default Re: how to give vlaues to array objects without using loop?

    In stead of guessing, I suggest you go google java arrays (or better: look it up in the book on Java you -must- have, right?). You're not going to get anywhere if you don't read and study.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,600
    Rep Power
    5

    Default Re: how to give vlaues to array objects without using loop?

    A good place to start is in the Java tutorials in my signature.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Creating multiple Objects using for loop.
    By thelastfoiter in forum New To Java
    Replies: 8
    Last Post: 11-29-2013, 09:37 PM
  2. create new Objects with for loop
    By nome in forum New To Java
    Replies: 4
    Last Post: 02-28-2012, 02:26 AM
  3. Replies: 1
    Last Post: 10-01-2011, 02:12 AM
  4. For loop to create objects
    By SteroidalPsycho in forum New To Java
    Replies: 4
    Last Post: 02-24-2010, 09:31 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •