Hello, I'm looking for help with a certain part of a program where the
program reads integers from a file into a vector, and then prints out the
sorted list of integers.
I'm trying to make the loop that checks to see if the vector is empty
(vector.isEmpty(), and if it is it puts the integer into the vector, else it
insertsElementAt before the point of the vector where the new integer is
less than the integer already there.
Heres what I have so far, that reads the data into the vector and prints it
out, but doesn't put it in order using a loop. Not quite sure what the
structure of the loop
import java.util.*;
import java.io.*;
public class BldList
{
public static void main (String[] args) throws FileNotFoundException,
IOException
{
Vector intList = new Vector(20);
BufferedReader inFile = new BufferedReader(new FileReader("numbers.txt"));
String input = inFile.readLine();
while(input != null )
{
int num = Integer.parseInt(input);
Integer newInt = new Integer(num);
intList.add(newInt);
input = inFile.readLine();
}
System.out.println(intList);
}
}
Woebegone - 27 May 2004 04:03 GMT
> Hello, I'm looking for help with a certain part of a program where the
> program reads integers from a file into a vector, and then prints out the
[quoted text clipped - 4 lines]
> insertsElementAt before the point of the vector where the new integer is
> less than the integer already there.
Is it a requirement that you maintain the Vector in sorted order? If not,
you might consider reading the whole file, and studying
<http://java.sun.com/j2se/1.4.2/docs/api/java/util/Collections.html#sort(jav
a.util.List)>, bearing in mind that a Vector is-a List. This way you also
don't have to worry about whether the vector is empty -- just sort it prior
to output.
If your requirement is to maintain a sorted list, you might want to consider
a different Collection, something like a TreeSet.
To the original question, assuming you must use a Vector and it is
maintained in ascending order, you'll have to find the index of the first
element greater than the one you're inserting, and then insert the new
element at that index. Thankfully, Vector handles the reorganization for you
then. You can use compareTo from the Integer class. It will be something
like:
while there are more elements in vector and not inserted
if newElement.compareTo(currentElement) is less than zero
insert newElement at current index
set inserted true
else
advance index
now, if current index equal to intList.size() and inserted still false
add newElement to intList
If you design your loop condition carefully you shouldn't have to test for
emptiness explicitly -- regardless of how many elements it has, you'll want
to insert (add) the element when the current index is equal to
intList.size(). The expression you use with compareTo will look something
like
newElement.compareTo((Comparable)intList.elementAt(currentIndex)).
HTH!
Sean.
Pete - 28 May 2004 06:58 GMT
Wouldn't it be easier to implement intList as an array rather than an
object?
It looks to me like you should just read the integers from the file into an
array, sort them there and just spit them out to the screen. Once you have
sorted them then insert them into the Vector object. Do you have to use the
Vector class? Or perhaps you could extend the Vector Class with BldLst to
include a sort method? If it doesn't already exist.
Also is numbers.txt just a string of integers?
I have sent you a revised BldList.java that makes no reference to the Vector
class. If it doesn't confuse you it may at least give you some insight into
the sorting algorithm you're seeking. I have used a string tokenizer to chop
it into bits and a NumFormat exception handler that wasn't needed in the
end.(so i just commented it out...makes no difference) Now you can just
create the Vector object using the already sorted list. Or you could just
paste the sort() method into the Vector class and throw your unsorted list
in there. There we go. Hope I haven't missed the point completely.
If you can post or send the Vector class source maybe it will make things a
bit clearer.
Cheers
Pete
import java.util.*;
import java.io.*;
public class BldList
{
public static void main (String[] args) throws FileNotFoundException,
IOException
{
int arrayLength = 20;
int [] intList = new int[arrayLength];
int x = 0;
BufferedReader inFile = new BufferedReader(new
FileReader("numbers.txt"));
String input = inFile.readLine();
//try
//{
while(input != null )
{
StringTokenizer st = new StringTokenizer(input);
while(st.hasMoreTokens())
{
intList[x] = Integer.parseInt(st.nextToken());
x++;
}
input = inFile.readLine();
}
//}catch(NumberFormatException e)
//{}
sort(intList, arrayLength);
for(int n = 0; n < arrayLength; n++)
{
System.out.println(intList[n]);
}
//you could then create the Vector object here
}
public static void sort(int [] list, int arrayLength)
{
int swap = 0;
for(int x = 0; x < arrayLength; x++)
{
for(int y = x + 1; y < arrayLength; y++)
{
if(list[x] > list[y])
{
swap = list[x];
list[x] = list[y];
list[y] = swap;
}
}
}
}
}