Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

best way to loop throught an array?

loop array

  • Please log in to reply
9 replies to this topic

#1 even821

even821

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 132 posts

Posted 29 October 2012 - 01:45 AM

right now i am using a for loop to iterate through an array, and i was wondering: is there any faster, more optimized way to do this? or is a for loop my best bet?
  • 0

#2 Orjan

Orjan

    CC Mentor

  • Moderator
  • 2918 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#

Posted 29 October 2012 - 01:50 AM

it's probably the best way.
  • 0

I'm a System developer at XLENT Consultant Group mainly working with SugarCRM.
Please DO NOT send mail or PM to me with programming questions, post them in the appropriate forum instead, where I and others can answer you.


#3 even821

even821

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 132 posts

Posted 29 October 2012 - 01:51 AM

ok :)
sorry for the stupid question btw :P
  • 0

#4 brbcoding

brbcoding

    CC Regular

  • Member
  • PipPipPip
  • 33 posts
  • Location:Florida
  • Programming Language:Java, C#, PHP, Python, JavaScript, Ruby, PL/SQL
  • Learning:Perl, Ruby

Posted 29 October 2012 - 05:56 AM

ok :)
sorry for the stupid question btw :P


The only stupid questions are the ones you fail to ask... :)
  • 0

#5 even821

even821

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 132 posts

Posted 29 October 2012 - 05:57 AM

haha thats true i guess :)
  • 0

#6 VNFox

VNFox

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 648 posts
  • Programming Language:C#, PHP
  • Learning:Assembly

Posted 29 October 2012 - 07:03 PM

I think you can do the "break" statement ... when you found your item. So that it doesn't loop anymore. If you are lucky ... your item might be on the top ... worst case is the item is at the very end.
  • 0

www.pickmike.com
I don't just develop software. I find solutions to your business needs.


#7 gregwarner

gregwarner

    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1586 posts
  • Location:Arkansas
  • Programming Language:C, Java, C++, C#, PHP, Transact-SQL

Posted 30 October 2012 - 07:28 AM

If your data in the array is sorted, you may use a faster algorithm such as a binary search. You find the middle element and compare it with your target value. If the value found at the midpoint is too large, you recursively search the smaller half of the array. If it's too small, you recursively search the larger half of the array. This has a running time of O(log n) rather than O(n) like a sequential search.

However, if you absolutely must visit each element of the array for some reason (if it's unsorted, or you need to process every element), then there's no faster way than O(n), the sequential search.
  • 0

ti-99-sig.png
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#8 Orjan

Orjan

    CC Mentor

  • Moderator
  • 2918 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#

Posted 30 October 2012 - 10:07 AM

You are all assuming the OP is looking for one element in the array. The OP maybe want to perform something on each element in the array?
  • 0

I'm a System developer at XLENT Consultant Group mainly working with SugarCRM.
Please DO NOT send mail or PM to me with programming questions, post them in the appropriate forum instead, where I and others can answer you.


#9 gregwarner

gregwarner

    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1586 posts
  • Location:Arkansas
  • Programming Language:C, Java, C++, C#, PHP, Transact-SQL

Posted 30 October 2012 - 10:14 AM

Because the OP didn't specify what his purpose was, I'm offering alternatives just in case. I did mention in my previous post that visiting every element cannot be performed any faster than O(n).
  • 0

ti-99-sig.png
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#10 Orjan

Orjan

    CC Mentor

  • Moderator
  • 2918 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#

Posted 30 October 2012 - 01:44 PM

Yeah, I just wanted to raise the thought to open alternatives
  • 0

I'm a System developer at XLENT Consultant Group mainly working with SugarCRM.
Please DO NOT send mail or PM to me with programming questions, post them in the appropriate forum instead, where I and others can answer you.






Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download