jGuru Forums

sorting elements in vector
1 posts in topic
Flat View
TOPIC ACTIONS:

Posted By:   Sharen_Su
Posted On:   Friday, July 12, 2002 06:21 PM

I need to calcuate median of a number of elements.The elements are already taken out from the vector.But the problem is i couldnt sort the elements.Is there a way to sort elements?I tried using Collections.sort..and put the element into List but it couldn't work as List is an interface. Can someone guide me and show me the coding?? thanks... if (operation[j].equalsIgnoreCase("median")) { double red; String redValues; for (int i=1; i { redValues = (String)(rValues.elementAt(i)); red = Double.parseDouble(redValues); out.println("Sequence before sorting:"+red); List rd = new List(); rd.add(red); Collections.sort(rd); out.println(&quo   More>>

I need to calcuate median of a number of elements.The elements are already taken out from the vector.But the problem is i couldnt sort the elements.Is there a way to sort elements?I tried using Collections.sort..and put the element into List but it couldn't work as List is an interface. Can someone guide me and show me the coding?? thanks...

```
if (operation[j].equalsIgnoreCase("median"))

{    double red;

String redValues;

for (int i=1; i

{

redValues = (String)(rValues.elementAt(i));

red = Double.parseDouble(redValues);

out.println("Sequence before sorting:"+red);

List rd = new List();

Collections.sort(rd);

out.println("

sortin = "+Collections.sort(rd));

}

```

i know the later part of the codes is wrong.. but can somebody kindly help me out? thanks...    <<Less

Re: sorting elements in vector

Posted By:   Anonymous
Posted On:   Friday, July 12, 2002 10:25 PM

`Hi Sharen,  I think the following code will solve problem.import java.util.Vector;import java.util.Collections;public class SortTest {	public static void main(String as[]) {		Vector v = new Vector();		v.add(new Integer(1));		v.add(new Integer(12));		v.add(new Integer(10));		v.add(new Integer(9));		v.add(new Integer(6));		v.add(new Integer(4));		System.out.println("Before sorting");		for(int i=0;i			System.out.println("Element at "+i+" is : "+v.elementAt(i));		Collections.sort(v);		System.out.println("");		System.out.println("After sorting");		for(int i=0;i			System.out.println("Element at "+i+" is : "+v.elementAt(i));	}}    --regards,      Shanmy`