Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By Norm

Thread: make elements

  1. #1
    casta is offline Member
    Join Date
    Apr 2016
    Posts
    27
    Rep Power
    0

    Default make elements

    Hope you can help me. In previous online exercises, I was able to make elements of an object by hand:
    Java Code:
    A A1 = new A();
    A A2 = new A();
    B B1 = new B();
    Now, I would like to make as much elements of B, as there are in the list of A. Each element of B should be different, and each element of B should have its own reference variable. How could I do that?
    Java Code:
    	public static void makeB(List<A> listOfA) {
    		for (A a : listOfA)
    		{
    			new B(); //HOW??
    		}
    	}
    Last edited by casta; 05-18-2016 at 05:19 PM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: make elements

    To do that you only need a count of the elements in the list of A (the elements themselves are not used). So simply use a for loop
    with a limit based on listOfA.size() and add the new B elements to a new List implementation.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: make elements

    Note: The posted code has several syntax errors in it that make the code harder to understand.
    A A1 = new A; // missing ()s after A

    for (Egg A : listOfA) // If listOfA contains A objects then "Egg A" is wrong. I should be "A egg"

    When posting code you should try to post valid code so we can know how good your java knowledge is.
    Last edited by Norm; 05-18-2016 at 04:17 PM.
    casta likes this.
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    casta is offline Member
    Join Date
    Apr 2016
    Posts
    27
    Rep Power
    0

    Default Re: make elements

    Quote Originally Posted by jim829 View Post
    To do that you only need a count of the elements in the list of A (the elements themselves are not used). So simply use a for loop
    with a limit based on listOfA.size() and add the new B elements to a new List implementation.
    But how could I instantly give each element a reference variable?

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: make elements

    how could I instantly give each element a reference variable?
    You can't. Variable names are created when the programmer types them into the source file.

    A reference to an instance of a class can be saved in a collection like an ArrayList or in an array.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    casta is offline Member
    Join Date
    Apr 2016
    Posts
    27
    Rep Power
    0

    Default Re: make elements

    Quote Originally Posted by Norm View Post
    Note: The posted code has several syntax errors in it that make the code harder to understand.
    A A1 = new A; // missing ()s after A

    for (Egg A : listOfA) // If listOfA contains A objects then "Egg A" is wrong. I should be "A egg"

    When posting code you should try to post valid code so we can know how good your java knowledge is.
    Thank you for your reply. I wanted to sketch the situation, but I was very inaccurate in my expressions. Sorry for that!

  7. #7
    casta is offline Member
    Join Date
    Apr 2016
    Posts
    27
    Rep Power
    0

    Default Re: make elements

    Thanks Norm. That was what I wanted to know!

Similar Threads

  1. How to make a make a Console program?
    By Snake006 in forum New To Java
    Replies: 7
    Last Post: 11-11-2014, 12:17 PM
  2. Replies: 22
    Last Post: 12-16-2011, 05:26 PM
  3. Replies: 3
    Last Post: 10-19-2011, 11:42 PM
  4. Replies: 4
    Last Post: 07-23-2011, 08:08 PM

Posting Permissions

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