I usually visit the java forum several times a day to read over posts and ask questions. At this point in my programming "journey" I glean lots more information from the forum than I give. I want so badly to be able to give back to the forum and help the less experienced. Unfortunately, I'm not at that point yet. There was a post in the java forum the other day that involved placing ints in an array a certain way:arranging array
One member posted this solution:
import java.util.Arrays;
public class FunnyNumberSorter {
public static void main(String[] args) {
int[] n = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
int[] sorted = new int[n.length];
final int center = (n.length / 2) - 1 + (n.length % 2);
for(int i = 1, sgn = -1; i <= n.length; i++, sgn *= -1) {
sorted[center + (sgn*i/2)] = n[i-1];
}
System.out.println(Arrays.toString(sorted));
}
}
I didn't post any code, but on my own, I came up with a solution:
class ArraySort
{
public static void main( String[] args )
{
int[] num = new int[ ]{ 10, 7, 5, 3, 1};
int middle = num.length/2;
int[] numTransfer = new int[ num.length ];
for( int i = 0, j = middle + 1, k = middle - 1; i < num.length; i++ )
{
if ( i == 0 )
{
numTransfer[ middle ] = num[ 0 ];
}
else if( i % 2 == 0 )
{
numTransfer[ k ] = num [ i ];
k--;
}
else
{
numTransfer[ j ] = num [ i ];
j++;
}
}
}
}
Something to note is that my solution does not account for arrays that have an even length. My code will only handle arrays with odd length. The other poster's code will handle both cases in a much simpler, cleaner, compact, and more elegant way. My code does work like it should for odd sized arrays. However, I feel that it is clumsy, bulky, ugly, inefficient etc.
I knew what the algorithm for placing ints in the new array should have been. However, this translated to the slog of code that I posted. I never even thought of determining the center point of the array like the other poster did:
final int center = (n.length / 2) - 1 + (n.length % 2);
I still consider myself a beginner programmer as I've been programming for less than a year. I feel that I am way better than when I first started, but when I see other people's code, I can get discouraged. Generally speaking, at what point should I start to see improvements in my code?
I absolutely love coding, but after reading a few articles online that point out that good programmers are born and not made, I wonder if I have what it takes to improve. These same articles drive home the point that you will make your improvements the first 3 to 4 years of coding.
Like I said, I love programming. Alot. I just wonder if I have what it takes to produce good code or if all I will ever be capable of is clunky mediocre code. It seems that I go through phases where I feel pretty good about my skills, and phases where I feel badly about them.
If you've made it to the end, I applaud you. Tell me what you think.
Thanks

New Topic/Question
Reply



MultiQuote










|