Betsy Camel

Ranch Hand

Posts: 119

posted 11 years ago

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.

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

Posts: 73

posted 11 years ago

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);

}

}

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

Posts: 119

posted 11 years ago

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].

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

Posts: 105

posted 11 years ago

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

jiju ka

Ranch Hand

Posts: 308

posted 11 years ago

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

if so..

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

posted 11 years ago

As I said before the arraySize is not accurate.

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 ]

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 ]

Don't get me started about those stupid light bulbs. |