aspose file tools*
The moose likes Beginning Java and the fly likes sorting in vectors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "sorting in vectors" Watch "sorting in vectors" New topic
Author

sorting in vectors

Betsy Camel
Ranch Hand

Joined: Oct 02, 2003
Posts: 119
hi
i am having a vector. i want to store the first value in the vector as "A", second as "B" and third as "c".
The fourth value should again be set as "a" and so on.


eg


A[0] =vector.elementAt(0);
B[0]=vector.elementAt(1);
c[0]=vector.elementAt(3);
a[1]=vector.elementAt(4);
and so on... Please let me know how to do this.
Kartik Patel
Ranch Hand

Joined: Sep 12, 2005
Posts: 73
This is the code you want

import java.util.*;

class sortVector
{

public static void main(String args[])
{
Vector vect=new Vector();
vect.add("c");
vect.add("f");
vect.add("k");
vect.add("a");
vect.add("g");
vect.add("z");

System.out.println("Vector before sorting--"+vect);
Collections.sort(vect);
System.out.println("Vector before sorting--"+vect);

}

}


Chop your own wood, and it will warm you twice. - Henry Ford
Betsy Camel
Ranch Hand

Joined: Oct 02, 2003
Posts: 119
I guess you have not understood the question i was asking. Please go through the question before you try to answer.


I am having a vector with values in it. the values have to be assigned

in this manner, depending on the size


a[0] = vector.elementAt[0];
b[0] =vector.elementAt[1];
and so on...where a & b are arrays or strings.

the fourth value from vector is again assigned to a[1].
Rajasekar Elango
Ranch Hand

Joined: Sep 13, 2004
Posts: 105
I guess simple modulus will do the trick..

eg: a[index % length] = vector.getElementAt(index)

Regards,
Raja.


SCJP 1.4
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61653
    
  67

Please be sure to post questions in the appropriate forum. Moved to Java in General.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
jiju ka
Ranch Hand

Joined: Oct 12, 2004
Posts: 306

i am having a vector. i want to store the first value in the vector as "A", second as "B" and third as "c".
The fourth value should again be set as "a" and so on.


eg


A[0] =vector.elementAt(0);
B[0]=vector.elementAt(1);
c[0]=vector.elementAt(3);
a[1]=vector.elementAt(4);


Your question is confusing. Sorry to say that. Let me ask you this


Are you asking how to distribute the values in vector among three arrays?


if so..


int collSize = vector.size();

arraySize = collSize/3 + 1; //not accurate though, consider size = 10

int[] a = new int[arraySize];
int[] b = new int[arraySize];
int[] c = new int[arraySize];
int aCount = bCunt = cCount = 0;

for each element in vector indexed by i; range 0 to size -1
{
currElement = vector.element at i;
if(i % 3 == 0)
{
a[aCount ++] = currElement;
}
else if (i % 3 == 1)
{
b[bCount ++] = currElement;
}
else
{
c[cCount ++] = currElement;
}


}

jiju ka
Ranch Hand

Joined: Oct 12, 2004
Posts: 306
As I said before the arraySize is not accurate.

arraySize = collSize/3 + 1; //not accurate though, consider size = 10

int[] a = new int[arraySize];
int[] b = new int[arraySize];
int[] c = new int[arraySize];


Below is a solution to compute right array size.
In case of 10 elements you need three arrays with 4, 3, 3 elements respectively
In case of 11 elements you need three arrays with 4, 4, 3 elements respectively
In case of 12 elements you need three arrays with 4, 4, 4 elements respectively

So the ArraySize can be different for all arrays.
Say aArraySize, BArraySize, cArraySize

After dividing the size by 3 you get a fraction,
If this fraction is zero (3/3), aArraySize = bArraySize = cArraySize = size/3;
If this fraction is .33 (1/3) or less than .5
aArraySize = size/3 + 1; bArraySize = cArraySize = size/3;
If this fraction is .66(2/3) or greater than .5
aArraySize = bArraySize = size/3 + 1; cArraySize = size/3;

To find the fraction you can do

fraction = size/3 - (int)size/3

[ December 21, 2005: Message edited by: jiju ka ]
[ December 21, 2005: Message edited by: jiju ka ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: sorting in vectors