Results 1 to 10 of 10
Thread: For loop problem
- 03-24-2012, 11:50 AM #1
Senior Member
- Join Date
- Nov 2011
- Posts
- 116
- Rep Power
- 0
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.
- 03-24-2012, 12:46 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 03-25-2012, 02:18 AM #3
Senior Member
- Join Date
- Nov 2011
- Posts
- 116
- Rep Power
- 0
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
- 03-25-2012, 12:29 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 03-25-2012, 02:15 PM #5
Senior Member
- Join Date
- Nov 2011
- Posts
- 116
- Rep Power
- 0
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
[/code]Java Code://Loop through 1 to 65535.. for(int i=1;i<=65535;i++) { //Create TCPScan object ArrayList ArrayList<TCPScan> tcpList = new ArrayList<TCPScan>(); }
- 03-25-2012, 03:26 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 03-25-2012, 03:40 PM #7
Senior Member
- Join Date
- Nov 2011
- Posts
- 116
- Rep Power
- 0
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); }
- 03-25-2012, 03:52 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
Re: For loop problem
That list is empty, so your advanced for-loop has nothing to iterate over; try something like this:
kind regards,Java Code:List<TCPScan> list= new ArrayList<TCPScan>(); for (int i= 1; i <= 65531; i+= 5) list.add(new TCPScan(i, i+4));
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 03-25-2012, 04:49 PM #9
Senior Member
- Join Date
- Nov 2011
- Posts
- 116
- Rep Power
- 0
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
- 03-25-2012, 05:51 PM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
Re: For loop problem
When people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
problem with loop
By pietr0 in forum New To JavaReplies: 7Last Post: 12-31-2011, 04:58 PM -
Need help with Loop Problem
By jayfaxva in forum New To JavaReplies: 2Last Post: 11-21-2011, 03:33 PM -
Problem with while loop, assigning a variable with a different value every loop? Help
By JavaProg in forum New To JavaReplies: 2Last Post: 11-07-2011, 02:25 AM -
problem with loop (maybe?)
By jdg951 in forum New To JavaReplies: 7Last Post: 12-08-2010, 10:32 AM -
simple line problem / for loop problem
By helpisontheway in forum New To JavaReplies: 1Last Post: 11-17-2009, 06:12 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks