patternjavaMinor
Dynamic array sorting and storing program
Viewed 0 times
sortingarrayprogramdynamicandstoring
Problem
I have written a program which closely resembles the way contacts are added and dynamically sorted in ascending order in your phone or other device. Though I have used
Integers instead of String, I was just wondering if this has any real world use and how I can improve it.import java.util.*;
public class Ascending
{
ArrayList array = new ArrayList();
public Ascending()
{
array.add(2);
array.add(4);
array.add(6);
array.add(8);
}
public static void main()
{
Ascending obj = new Ascending();
obj.ascendingOrder();
}
public void ascendingOrder()
{
for(int loop=0;looparray.get(last))
{
array.add(size,input);
System.out.println("\nSorted array -:");
for(int q:array)
{
System.out.println(q);
}
}
}
}
}Solution
import java.util.*;It's often preferred to do separate imports rather than import an entire group. Some disagree with this. You can see both viewpoints argued at https://stackoverflow.com/questions/147454/why-is-using-a-wild-card-with-a-java-import-statement-bad
ArrayList array = new ArrayList();It's generally preferred to use the interface on the left side, as so
List numbers = new ArrayList();It won't make much difference in a program like this, but in a program where you pass your field as a method parameter, it can make it much easier to change implementations in the future. It also encourages you to think about whether or not to use implementation specific functionality.
I prefer to use a more specific name than
array. Of course, in this case, you don't have much of one as the field is pretty generic. I might also go with data. Some would prefer array to these names. public Ascending()
{
array.add(2);
array.add(4);
array.add(6);
array.add(8);
}Is that test code in your constructor? That's not the place for it. Consider
public Ascending(List initial)
{
numbers.addAll(initial);
}or better yet, something like
public Ascending(List initialData)
{
for (Integer number : initialData)
{
numbers.add(number);
}
}As that makes sure that the order is correct (assuming
add is implemented correctly). Then you can change the values in the calling code rather than editing the class itself. I.e. change
Ascending obj = new Ascending();to something like
Ascending ascending = new Ascending(Arrays.asList(2, 4, 6, 8));Alternately, you could define a new method
add and call it in main. This would also allow you to pull some code out of your ascendingOrder method. public void ascendingOrder()Usually methods have verb names. Since this takes input, I might call it something like
addFromInput. Also consider breaking this into three parts: a
for loop in main; an input process method; and an add method. for(int loop=0;loop<=10;loop++)
{
Scanner sc = new Scanner(System.in);You don't need to make a new
Scanner for every iteration. Do it once prior to starting the loop: Scanner sc = new Scanner(System.in);
for (int loop = 0; loop <= 10; loop++)
{I also added some whitespace in the
for loop declaration. It makes it easier for me to read (the computer won't care). System.out.println("The array is 2,4,6,8 ");Consider writing a
toString method so that you can just say System.out.println("The array is " + this);Then it will output the current array on every iteration rather than outputting the original array. This also allows you to change the initial values in only one place.
Alternately, you can write a
print method that does the same thing. The toString is more flexible though. int i,size = array.size(),last=size-1;
for(i=0;iarray.get(last))
{
array.add(size,input);
System.out.println("\nSorted array -:");
for(int q:array)
{
System.out.println(q);
}
}The indent looks wrong at the end there. However, this whole block of code can be replaced with
add(input);
System.out.println("\nSorted array -: " + this);if you define
add and toString as previously discussed. The
add method can be defined as public void add(Integer number)
{
int i = 0;
int size = numbers.size();
while (i < size && number < numbers.get(i))
{
i++;
}
numbers.add(i, number);
}or
public void add(Integer new)
{
int i = 0;
for (Integer number : numbers)
{
if (new < number) {
numbers.add(i, new);
return;
}
i++;
}
numbers.add(new);
}among other implementations.
Note that I don't do your
last check. It's unnecessary in both versions. In the first version,
i always equals the correct place in the list when it exits the loop. In the second version, it never reaches the code outside the loop unless it has already checked every previous location. Note that when used with just one argument,
add appends the element to the list. This has the same effect as numbers.add(size, new); but is shorter to write. Moving the printing of the array out of the
add process simplifies the logic as well and saves duplicating code.Code Snippets
import java.util.*;ArrayList<Integer> array = new ArrayList<Integer>();List<Integer> numbers = new ArrayList<Integer>();public Ascending()
{
array.add(2);
array.add(4);
array.add(6);
array.add(8);
}public Ascending(List<Integer> initial)
{
numbers.addAll(initial);
}Context
StackExchange Code Review Q#112960, answer score: 3
Revisions (0)
No revisions yet.