Arrays

An array is an ordered data structure consisting of a collection of elements (values, variables or references), each identified by one or more indexes

For-loop

A for loop is a control structure used by many programming languages to iterate over a range

Length index version

Quality Example
Greater

"Also change your logic in the for-loop to be not since i will not ever be greater than the arrays length"

from question "If else in for loop over array javascript"

Less

"I fixed this by adding after the for-loop and before the return statement which fixes the problem but if the for-loop is written to continue while i is less than the arrays length and when the arrays only contains a the length is one and i is 0 shouldn t it also pop a"

from question "Reversing array, pop method in loop not popping to completion"

"When i try to access it with a for-loop where its index is less than the arrays length i get the following error message typeerror function object is unsubscriptable"

from question "TypeError: 'function' object is unsubscriptable in Python?"

"If the length of the arrays is less than 8 a regular for-loop summation is performed"

from question "Weird numpy.sum behavior when adding zeros"

Bigger

"It turns out i had a hard coded maximum index in my for-loop which was bigger than the arrays i was trying to assign to"

from question "Lvalue in assignment too complex"

Much larger

"Above is a simplified version of my actual code where the c arrays is much larger so i have to use a for-loop to get every index"

from question "How to access numpy array with a set of indices stored in another numpy array?"

Size user program

Quality Example
Less

"Okay so after telling the user to enter the size of the arrays the arrays was assigned to lets say 5 therefore in the first for-loop the program is checking if i is less than the arrays size so it can only be entered 5 times as that is the size of the arrays"

from question "How to determine how big array is and then display all the information inside?"

"But the for-loop is not reading writing the last part of the parent file which is less than the arrays size"

from question "Splitting a .gz file into specified file sizes in Java using byte[] array"

Faster foreach-loop better

Quality Example
Shorter

"Because if the first word in arrays is shorter than second one you need second for-loop"

from question "Return true if the string in the first element of the array contains all of the letters of the string in the second element of the array?"

"In this case if any element in arrays is shorter than 11 symbols will become big and for-loop will stop executing"

from question "When to use 'Break' in For loop?"

More

"Im going to have a second for-loop that makes more arrays and names these arrays as the strings from my previous arrays"

from question "Name an array with a preexisting string"

Easier

"With one arrays one can do which is easier than a for-loop"

from question "Easy way of printing two numpy arrays with each element in a different line?"

Nothing more

"Looping with a for-loop is nothing more than a basic iteration of an arrays using a for-loop"

from question "JavaScript code for combining for loop, text, checkboxes, result"

Smaller

"As mentioned above as you remove items the arrays gets smaller so a for-loop is probably not the best solution"

from question "Python:IndexError: list index out of range in python"

Faster

"For example sometimes a for-loop is faster than the built-in arrays methods in some browsers"

from question "Combine same-index objects of two arrays"

"The for-loop is faster than the foreach-loop if the arrays must only be"

from question "* (no title is found for this review)"

"This is the reason why working with the higher-dimensional arrays ends up being so much faster than the for-loop -based code"

from question "Numpy multiplication of vectors of different size avoiding for loops"

"Would an arrays be faster than a for-loop in this case"

from question "Automate Text Manipulation transform, combine, and separate in Excel 2007 using arrays"

"Thus the for-loop is faster than the foreach-loop if the arrays must"

from question "Why is my foreach faster than my for loop?"

"I completely failed to check that assertion and just jumped into the analysis of how the enhanced for-loop is faster on arrays than lists"

from question "Why is iteration through List<String> slower than split string and iterate over StringBuilder?"

"I tried this code with my the big arrays it is around 10 to 20 times faster than a for-loop solution and around 200 times fast than the old code"

from question "Np.cross produces wrong results, search for a working alternative"

"The for-loop is faster than the foreach-loop if the arrays must only be accessed once per iteration"

from question "Advantages to using foreach compared to for"

"Iterating pair-wise you d normally do something like but iterating over an arrays is faster than using a c-style for-loop"

from question "How to do fast comparison of arrays in perl"

Method faster way

"When you know both objects are arrays method is a faster way to check equality than for-loop"

from question "Which one is faster? for-loop or isEqualToArray"

Better

"Edit using an enhanced for-loop is a lot better than using arrays"

from question "Cartesian Products with sets in java"

"But when looping through an arrays it s better to use a regular for-loop"

from question "Difference between for loop and for-in loop in javascript"

"The questions should i use for-loop or is it better to do it recursively next insert will be called after previous is done should i check if saved.length datalength and then return the arrays or is there some better way how to do it"

from question "NodeJS - How to insert multiple rows to MySQL"

"But you will have loops the outer arrays in order to get it to work which would be better done with a for-loop so you reference both at once"

from question "Merge two arrays, but not like array_merge"

"Since it s an arrays it s better to use a for-loop with a counter variable i which starts from 1"

from question "Javascript: use array.slice() in a loop and not work as expected"

"But for the arrays it is better to use for-loop as shown by alnitak than for-in"

from question "Javascript type issue: Uncaught TypeError: Object 0 has no method 'draw'"

Far better

"But then normal for-loop is far better for arrays than using for-in which is actually for object"

from question "Why does adding a method to the Array prototype in javascript break iteration on a for loop?"

Much better

"You can use array.prototype.find method to check if the element exists in arrays which is much better than perform a traditional for-loop"

from question "Checking array object existence"

Longer iterations division

Quality Example
Longer

"Note that this is one of those cases where matrix division of large arrays takes longer than a for-loop"

from question "Independently solving for rows with mrdivide (B/A)?"

"The hits arrays is longer than numtotalhits so your for-loop limit should be numtotalhits instead of hits.length"

from question "Apache lucene search code prints null"

More iterations

"The longer the arrays gets the more iterations your for-loop will need"

from question "Is it faster to drop and recreate an array, or fill it with zeroes, and why?"

Straightforward opinion specific

Quality Example
Usually better

"In my opinion a for-loop is usually better for doing something a specific number of times and if you re just iterating over every item in an arrays foreach is usually more straightforward"

from question "How to populate array data into a HTML table"

Typically more straightforward

"In its implementation foreach executes a closure over every element in the arrays this is typically more straightforward and transparent alternative to old-fashioned for-loop"

from question "Swift 2 count duplicate in array"

Good small amount

Quality Example
Better speed

"Use a for-each loops to go through a range it s not as fast as using a variant arrays but keeps things simple and offers better speed than a for-loop"

from question "Copy rows to a new worksheet VBA"

Probably better

"I suppose the first option is good for small arrays but a for-loop is probably better practice as the amount of code would remain the same regardless of arrays size"

from question "* (no title is found for this review)"

Others

Quality Example
More suitable

"A for-loop is more suitable to iterate through the arrays"

from question "How to read json array in php"

Better

"Also with the for-loop it s considered better to limit the scope of the iterating variable i and to use println you need system.out not just system and you need a string java arrays do not override tostring so something to output the numbers the user entered after loops like"

from question "Issues with a for loop in a user entered array"

Less memory

"In such a simple arrays you shouldn t be concerned about memory usage but the for-loop consumes less memory than foreach because foreach uses an internal copy of the arrays"

from question "PHP arrays for looping on memory usage"

Probably closer as

"Try catch inside for-loop is probably closer as long as you don t mind the unassigned values left in the arrays"

from question "Error checking in java while converting a string to a double"

Simpler

"Note that you should check if index is within arrays bounds in such cases and that system.arraycopy is more efficient and arguably simpler than a for-loop for copying arrays"

from question "Java : Save Assigned String from method in an array"

4x faster

"Running a quick benchmark it seems that the for-loop is 4x faster even in the worst case where you have to replace every single time and you construct a new arrays to hold the replacements"

from question "Replace a given value in array with values from second array in C#"

Slower

"In this case instead of generating two large matrices with the row and column indices you can use a for-loop on the rows of your arrays it s slower but not as slow as a double for-loop"

from question "Get Index of 2D array in Python"

"When summing an arrays over a specific axis the dedicated arrays method array.sum ax may actually be slower than a for-loop"

from question "Numpy.sum may be slower than Python for-loop"

Usually more information

"An arrays usually offers more information for alias analysis and after some optimizations the same code will be generated anyway search for-loop strength reduction if curious"

from question "Coding Practices which enable the compiler/optimizer to make a faster program"

Here more

"The for-loop here is more efficient for 2 reasons a you don t have to construct a temporary arrays of tuples like with zip and b it returns false as soon as a non-match is found"

from question "How can I extend Array so equality check of arrays of optionals are possible?"

More work

"But modifying the arrays is more work than a simple for-loop again"

from question "Laravel 5.4 Eloquent mass creation"

Less

"So when you go up an arrays 0-9 you want the top of a for-loop to be less than the arrays length when you are going down the arrays 9-0 you want the lower bound to be less than or equal to the bottom of the arrays otherwise you will start out trying to access at 10 the arrays length and get an outofrangeexception"

from question "Why am I getting an IndexOutOfRangeException in this code?"

Smaller

"To use this in a loops you can write a simple for-loop which always checks if the index stil is smaller than the arrays length"

from question "How to find an Object Array contains an element or not?"

Still more times

"But the outer for-loop still runs 9 more times pushing temp which is already a set number onto the numbers arrays"

from question "How to generate 10 unique random numbers from a set of 100 using javascript?"

Quicker

"To my knowledge and research so far - javascript s native for-loop is quicker than arrays map for iterating through the arrays"

from question "Build arrays to show index number"

Much better

"Notice that besides using the filter method or not even with a plain for-loop is much better for performance create a new arrays from scratch instead of mutate the current one multiple times"

from question "How to remove multiple items from an array via looping in Javascript"

Worse

"So then i think to go with that approach i would end up with a for-loop and loads of add calls but surely that is even worse than the arrays copy isn t it"

from question "What is the most efficient way to deal with methods that return variable length results into a fixed buffer?"

More

"A for-loop is more adequate than a do while for simply iterating an arrays string"

from question "Conversion of word to alphabetical values of letters c++"

"But i don t want to use a for-loop as my json arrays has more than 3 entries and if i request a lot of data in short time it takes long time till the for-loop goes through every entry"

from question "JSON: Getting entry with specific value"

"In your code on line in the for-loop does no more point to an arrays --the compiler has no information about the length of the variable that it s pointing at whether it is an arrays a single chars or an int or something else-- but it is just a plain pointer presumably a pointer pointing to a default int"

from question "C programming: get the size of a two-dimensional pointer to arrays"

"This is a scenario where a traditional for-loop is more handy than just iterating over the arrays"

from question "How to create for loop to add elements in an array that requires urls?"

"Next i can use 3rd 4th and 5th level of arrays means more than for-loop statement will be assign"

from question "Mongo DB C# code using 2 level , 3 level and 4 level of array"

Much more reliable

"I ve used it for convenience a for-loop is much more reliable for converting an htmlcollection to an arrays"

from question "Can't remove event listener from DOM element"

Ndx less

"On the last iteration of the outer for-loop ndx is one less than array.length so when you call arrays ndx+1 that is equivalent to arrays array.length which out of bounds since arrays start indexing at 0"

from question "Array Index Out Of Bounds error"

Faster

"It returns a byte arrays of all the pixels which can be iterated much faster than a for-loop with a call to getpixel inside nested inside another for-loop"

from question "AS3: How to access pixel data efficiently?"

"It is clear without any performance tests that native javascript for-loop is faster but there is no big difference for small arrays like 10-20 small items"

from question "What is the difference between for...in loop and jQuery each() function?"

Less time

"I am just starting to learn about the streams and parallel in java and i was wondering why a normal for-loop takes less time than intstream paralleled at adding items to an arrays"

from question "Why does IntStream.range(0, 100000).parallel.foreach take longer then normal for loop"

More efficient

"The only way to copy arrays that is more efficient than for-loop coding is system.arraycopy"

from question "Objects matrix of Strings to matrix strings in java"

Back to Home
Data comes from Stack Exchange with CC-BY-SA-3.0