Results 1 to 10 of 10
Like Tree1Likes
  • 1 Post By JosAH

Thread: For loop problem

  1. #1
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default For loop problem

    Hi,

    I'm trying to for loop through creating initialized constructors with different values on thousands upon thousands of time.

    My initial idea was creating constructors like so :

    TCPScan tcp1 = new TCPScan(1,5);
    TCPScan tcp2 = new TCPScan(6,10);
    TCPScan tcp3 = new TCPScan(11,15);
    .
    .
    .
    TCPScan tcp65500 = new TCPScan(65531,65535);

    But this will take a phenomenon amount of time.
    So I was thinking could I do this in a for loop in creating TCPScan constructors with the different values to 65535 as I was trying to do?
    Thanks much appreciated

    The last initialized constructor 'TCPScan tcp65500 = new TCPScan(65531,65535);' was a guess,, but in around that range.
    Last edited by dougie1809; 03-24-2012 at 11:53 AM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,513
    Blog Entries
    7
    Rep Power
    20

    Default Re: For loop problem

    Is the following induction correct? You want TCPScan objects for the values 5*i+1, 5*i+5 for i >= 0 and 5*i+5 <= 65535.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default Re: For loop problem

    Yes, its alot more easier just to say TCPScan object actually. I think that could be a right induction. So would that be done within a nested for loop, or how can it be implemented?
    Thanks

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,513
    Blog Entries
    7
    Rep Power
    20

    Default Re: For loop problem

    Well, use a for-loop and, say, a List<TCPScan> and fill the list in the body of your loop with the correct TCPScan objects ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default Re: For loop problem

    Ok. I have the following snipped of code below which only has a for loop to create a TCPScan ArrayList. I can't get my head around on applying your induction into it, and im not sure what values I have to fill in assigning TCPScan objects into the list?
    Thanks

    Java Code:
    //Loop through 1 to 65535..
    		for(int i=1;i<=65535;i++)
    		{
    			//Create TCPScan object ArrayList
    			ArrayList<TCPScan> tcpList = new ArrayList<TCPScan>(); 
    		}
    [/code]

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,513
    Blog Entries
    7
    Rep Power
    20

    Default Re: For loop problem

    That fragment creates 65535 ArrayLists of TCPScan objects; that is not what you want; you want to create a single list of TCPScan objects and your loop has to create those TCPScan objects and add them to that single list.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default Re: For loop problem

    Yes, that's a silly mistake. I have now created a single TCPScan ArrayList and then in the enhanced for loop I have TCPScan object being added to the tcpList, but I haven't specified the values based on your induction?
    Thanks

    Java Code:
    //Create TCPScan object ArrayList
    		ArrayList<TCPScan> tcpList = new ArrayList<TCPScan>();
    		
    		//Loop through the empy tcpList..
    		for(TCPScan currentTCPScan : tcpList)
    		{
    			//Trying to add individual TCPScan object with different values
    			tcpList.add(currentTCPScan); 
    		}

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,513
    Blog Entries
    7
    Rep Power
    20

    Default Re: For loop problem

    That list is empty, so your advanced for-loop has nothing to iterate over; try something like this:

    Java Code:
    List<TCPScan> list= new ArrayList<TCPScan>();
    for (int i= 1; i <= 65531; i+= 5)
       list.add(new TCPScan(i, i+4));
    kind regards,

    Jos
    dougie1809 likes this.
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default Re: For loop problem

    Oh that works perfectly. Thanks alot :)
    Two questions, how did you figure out to only loop as far as 65531? And what does the 'i+= 5' condition do?
    Thanks

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,513
    Blog Entries
    7
    Rep Power
    20

    Default Re: For loop problem

    Quote Originally Posted by dougie1809 View Post
    Oh that works perfectly. Thanks alot :)
    Two questions, how did you figure out to only loop as far as 65531? And what does the 'i+= 5' condition do?
    Thanks
    I guessed it from your original code (math folks call that 'induction') and 'i+= 5' adds the value 5 to the value of i and makes it the new value of i.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. problem with loop
    By pietr0 in forum New To Java
    Replies: 7
    Last Post: 12-31-2011, 04:58 PM
  2. Need help with Loop Problem
    By jayfaxva in forum New To Java
    Replies: 2
    Last Post: 11-21-2011, 03:33 PM
  3. Replies: 2
    Last Post: 11-07-2011, 02:25 AM
  4. problem with loop (maybe?)
    By jdg951 in forum New To Java
    Replies: 7
    Last Post: 12-08-2010, 10:32 AM
  5. simple line problem / for loop problem
    By helpisontheway in forum New To Java
    Replies: 1
    Last Post: 11-17-2009, 06:12 AM

Posting Permissions

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