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

Thread: How to build a List dynamically

  1. #1
    Junior Member
    Join Date
    Jul 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to build a List dynamically

    Dear Experts

    I am looping thru a Result Set from a SQL-Query using the 'while' statement. This is result set is "forward-only", which means can't call the 'first' method to loop it again. Actually I don't want to loop it twice anyway for performance reasons.

    Instead my idea is to copy the values into a collection or multi-dimensional array and then scan this list twice to build my final result. (which is a string)

    My idea was to use a nested class, but I got a compilation error. In C# one might use a 'struct' to get this done.

    What's the deal in Java?

    I'd ideally create something like

    public class CodeInfo (...3 properties) and then pass that to an arrayList to iterate it...but haven't got it done myself...

    Thanks
    Roger


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,963
    Thanks
    56
    Thanked 2,355 Times in 2,326 Posts

    Default Re: How to build a List dynamically

    Do you have a specific java programming question?

    I got a compilation error
    If you need help with the error, copy and paste it and the code that caused it.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    858
    Thanks
    0
    Thanked 158 Times in 141 Posts

    Default Re: How to build a List dynamically

    A struct is nothing other then a class without methods and only public attributes. If you know how to do it in C# you should also be able to do it in java since these two are hardly different.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,114
    My Mood
    Mellow
    Thanks
    244
    Thanked 755 Times in 741 Posts

    Default Re: How to build a List dynamically

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

  5. #5
    Junior Member
    Join Date
    Jul 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to build a List dynamically

    Well, I mastered my problem using a different approach, but I'm still interessted in this, as it's probably "basics":

     
    public class Playground {
     
    	public class InnerClass {
    		public String anything;
    		public Integer someNumber;
    	}
     
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
     
    		System.out.println("Test");
     
    		InnerClass innerClass = new InnerClass();
     
     
    		// final goal: Fill a Collection (ArrayList)
    		// with the InnerClass "struct/record"
    		// in a loop (while)
     
    	}
     
    }

    Eclipse (Luna, Java 1.7) is saying:

    No enclosing instance of type Playground is accessible. Must qualify the allocation with an enclosing instance of type Playground (e.g. x.new A() where x is an instance of Playground).

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,963
    Thanks
    56
    Thanked 2,355 Times in 2,326 Posts

    Default Re: How to build a List dynamically

    When posting errors, it helps if you also show the source line where that error happened.

    Take a look at the tutorial about inner classes: Nested Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Jul 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to build a List dynamically

    Of course, sorry it's the line where I try to instantiate the InnerClass (new)

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,963
    Thanks
    56
    Thanked 2,355 Times in 2,326 Posts

    Default Re: How to build a List dynamically

    Did you read the tutorial and try some of the things that were suggested?
    What about the suggestion in the error message you posted?

    One problem is trying to do too much in the main() method. Move everything to the class's constructor or methods.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Jun 2014
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to build a List dynamically

    Thanks guys, this was really helpful.

Similar Threads

  1. Difference between Maven Build and Eclipse Build
    By Beginner2java in forum Java IDEs
    Replies: 2
    Last Post: October 14th, 2013, 11:13 AM
  2. Difference between Maven Build and Eclipse Build
    By Beginner2java in forum Java IDEs
    Replies: 1
    Last Post: October 14th, 2013, 11:09 AM
  3. Difference between Maven Build and Eclipse Build
    By Beginner2java in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 14th, 2013, 11:09 AM
  4. Replies: 2
    Last Post: January 18th, 2013, 11:12 AM
  5. Replies: 5
    Last Post: January 9th, 2012, 02:36 PM