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 5 of 5

Thread: Creating Objects at specified indexes

  1. #1
    Member
    Join Date
    Feb 2013
    Posts
    57
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Question Creating Objects at specified indexes

    Hi,

    I am trying to add created objects to an ArrayList in a for-loop through indexes. The code below gives me errors when I try compile

    //Create 4 Question objects...
    Question ques1 = new Question("First", 1);
    Question ques2 = new Question("First", 2);
    Question ques3 = new Question("First", 3);
    Question ques4 = new Question("First", 4);
     
    //Create an empty Question ArrayList
    ArrayList<Question> list = new ArrayList<Question>();
     
    //for loop through 4 times starting at index 1
    for(int i=1; i<=4; i++)
    {
            //Trying to add each Question object along with the specified index during the loop.
    	list.add(ques+i);
    }

    The code outputs: cannot find symbol: variable ques

    Could anyone identify the problem?

    Thanks


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

    Default Re: Creating Objects at specified indexes

    cannot find symbol: variable ques
    The compiler can not find a variable named: ques
    Where is that variable defined?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Feb 2013
    Posts
    57
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Creating Objects at specified indexes

    I understand how the compiler cannot find ques, but I used a for loop to add each index to the ques variable (ques+i).

    So according to the compiler, wouldn't it look as if the variable is ques1, ques2, etc (where 1, 2, etc are the indexes)?

    Do you get where I am trying to get at?

    Thanks

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

    Default Re: Creating Objects at specified indexes

    Variable names are defined when you type them into the source code.
    If you want to change the contents of a String variable from "ques1" to "ques2" etc then enclose ques in "s like this:
    "ques" + 1

    You can use those Strings as keys in a Map to allow you to access objects using one of those Strings as the key.

    Otherwise define an array with the names of the Question variables.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Feb 2013
    Posts
    57
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Creating Objects at specified indexes

    Thanks, yea il try that so

Similar Threads

  1. creating Frame objects in an Applet
    By vk2goh in forum AWT / Java Swing
    Replies: 1
    Last Post: May 30th, 2012, 07:04 AM
  2. Creating classes with generic objects
    By rbt in forum Collections and Generics
    Replies: 2
    Last Post: April 23rd, 2012, 08:08 PM
  3. Creating multiple objects with a for loop
    By JackCannon15 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 20th, 2011, 07:26 PM
  4. Creating random objects
    By JackCannon15 in forum Object Oriented Programming
    Replies: 2
    Last Post: November 18th, 2011, 08:50 AM
  5. Loop not creating objects
    By xecure in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 30th, 2010, 10:48 PM