• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

sorting in vectors

 
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);

}

}
 
Betsy Camel
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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].
 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess simple modulus will do the trick..

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

Regards,
Raja.
 
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please be sure to post questions in the appropriate forum. Moved to Java in General.
 
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic