All Questions
Tagged with array coding-style
11 questions
3
votes
1
answer
275
views
Is it considered a bad practice to oversize an array so that negative indices behave well?
I have been practicing implementing some classic dynamic programming algorithms and was wondering if my implementation of the longest common subsequence search has a significant code smell.
In python,...
3
votes
2
answers
126
views
Should I move tasks which is just for a specific element only out of for loop?
For example, I have a for loop, which element 0 has additional function to run compared with other elements, my question is, should the additional function be:
1.place inside for loop
for(int i=0;i&...
3
votes
5
answers
8k
views
Check for empty PHP array
An empty array in PHP is considered falsey. That means the following will not print anything as the array is empty.
<?php
$myArray = array()
if ( $myArray ) {
print "My Array is NOT empty";
}
...
2
votes
3
answers
2k
views
What are pros and cons of using temporary "references"?
In C and C++ (and I guess other languages that allow taking a "reference" to an array element or something similar), when you have an array like type, accessing individual elements of such an array ...
2
votes
3
answers
3k
views
Return array pointers vs. populating an array inserted as a parameter?
Which is better? I noticed the latter is used in a lot of C code. People will typically malloc an array, and then pass that as a parameter to a function, which will then populate it. Whereas in Java, ...
2
votes
2
answers
509
views
Should I use Array or Set if both can be used to finish my task?
for example, Suppose I have a 2d array:
let filterArr=[
[1,1,0,1,1],
[0,1,1,1,0],
[1,1,0,1,0]
];
I want to find and store the index of column that all are 1, i.e.:position 1 and 3, and the ...
2
votes
1
answer
2k
views
How should I define hardcoded strings with some variable parts? Reuse more characters? Or keep the whole sentence?
for example, sometimes I need to define a hardcoded string with some variable parts, I often have trouble to choose the style:
style 1 : reuse every characters when possible
showMessage(num){
let ...
2
votes
2
answers
186
views
Should special case be inside or outside the for loop here?
For example, suppose I have 2 arrays:
let arr1=[5,2,1];
let arr2=["abcde","ab","a"];
my work is simple : to check if length of strings in arr2 are larger than corresponding element with same index in ...
2
votes
1
answer
2k
views
Why does Java support brackets behind variables and even behind method signatures? [closed]
Java allows this:
class X{
int i,j[]; // j is an array, i is not
}
and even worse, it allows this:
class X{
int foo(String bar)[][][] // foo actually returns int[][][]
{ return null; }
}...
1
vote
2
answers
171
views
Is it unnecessary to have a method that merely loops through an array and calls another method with each item?
For example, if there's multiple lines to be drawn, I could do
function drawLines(lines) {
lines.forEach(function (line) {
drawLine(line);
});
}
function drawLine(line) {
//...
}
...
0
votes
1
answer
164
views
For arrays, should I use the first position to store the selected element, instead of variable like "selectedIndex", if possible?
For example, I have a toggle button, which would show the current selected element, and would show next element when it is clicked, and it can loop back to first element. I have 2 ways to store the ...