Comparing elements in vector..
hi ,
how to compare elements in 2 vectors(diff size)
pls help
arvin
thks for your help,
for(int i=0;i<=v.size();i++)
if(v.elementAt(i).equals(v2.elementAt(i))
}when i did like this iam getting error
arvin
Similar Messages
-
Comparing elements inside a vector
Hello,
I am having a hard time understanding how to compare elements inside a vector. I have a vector of six strings and I want to compare the first element at index 0 with all other elements at positions 1-5. After that, compare the element at position 1 with elements at positions 2-5 and so on until all elements have been compared against each other.
Can some one please advise me on how to achieve this?
Thank you.joor_empire wrote:
Thanks for replying. I tried using nested loops, but I keep getting array index out of bound error.Hmmm, then you're doing it wrong.
For more information, you'll have to give us more info, such as your code (please use code tags when posting it though). -
hi,
I want to compare the float no in a vector e.g. [{0.2},{0.55}];
but I cannot just compare the 2 using x.elementAt(i) becoz this return object so
i do ix.elementAt(i) nstanceOf float but it pops up error,
but I am actually doing object instance of float, I am wondering where it was wrong
for(int i=1;i<x.size(); i++){
if(x.elementAt(i) instanceOf float){
if( x.elementAt(i)< x.elementAt(i-1)){
mean_dist.addElement(x.elementAt(i));
mean_dist.toString();
System.out.println("train data: "+ mean_dist);
anyone can give me idea
many thxyep, sure thing, when you use Vector.elementAt(index) it returns an Object, not a Float
but... you KNOW its a Float cos thats what you put there! ok?
Vector has to return Object as when the class was written there was no idea what you would use it for and Object is the general class that coveres just about everything..
what you need to do is "cast" to Float, that is saying,
"ok you dont know its a Float, but I do ( I hope) and I want you to deal with it as a Float"
the way you do this is Float gimme=(Float)myVector.elementAt(index);this tell the compiler that you expect a Float and should deal with the return as a Float
the compiler will do its best to use what it gets as a Float, but if it cant (ie if you put something that isnt a Float in myVector at (index)) then it will generate a ClassCastException
you dont HAVE to catch this, but you may if you want -
Compare newly added Vector Element with all previous
Hi all
I am adding Point2D elements to a vector "Numbers" in such a way that every newly randomly created element that is at a distance of 0.3 from all the previous points is added else not.
I have written a code that only checks from the last element but not all the previous elements.
Can someone check what wrong am I doing.
The code is runnable.
import java.awt.geom.Point2D;
import java.util.*;
public class Distance
Vector<Point2D> numbers = new Vector<Point2D>();
Random rnd = new Random();
void getList()
double xCoord,yCoord;
for(int i=0;i<5;i++)// is is the no. of coordinates stored later only "b" to be used
xCoord = rnd.nextDouble(); // X-coord of AP
yCoord = rnd.nextDouble(); // Y coord of AP
if(i<2)
numbers.addElement(new Point2D.Double(xCoord,yCoord));
System.out.println("0th n 1st element added : "+i+ " "+numbers.get(i));
else
for(int j=i-1;j>i-2;j--)
if((numbers.get(j).distance(xCoord,yCoord))>0.3)
numbers.addElement(new Point2D.Double(xCoord,yCoord));
System.out.println("next element added : "+i+ " "+numbers.get(j)+" with distance: "
+(numbers.get(j).distance(xCoord,yCoord))+"from "+ xCoord + " "+ yCoord);
Iterator it = numbers.iterator ();
while (it.hasNext ())
System.out.println(it.next());
public static void main(String[] argv)
Distance d = new Distance();
d.getList();
}Thanks a lotHi all
I am adding Point2D elements to a vector "Numbers" in such a way that every newly randomly created element that is at a distance of 0.3 from all the previous points is added else not.
I have written a code that only checks from the last element but not all the previous elements.
Can someone check what wrong am I doing.
The code is runnable.
import java.awt.geom.Point2D;
import java.util.*;
public class Distance
Vector<Point2D> numbers = new Vector<Point2D>();
Random rnd = new Random();
void getList()
double xCoord,yCoord;
for(int i=0;i<5;i++)// is is the no. of coordinates stored later only "b" to be used
xCoord = rnd.nextDouble(); // X-coord of AP
yCoord = rnd.nextDouble(); // Y coord of AP
if(i<2)
numbers.addElement(new Point2D.Double(xCoord,yCoord));
System.out.println("0th n 1st element added : "+i+ " "+numbers.get(i));
else
for(int j=i-1;j>i-2;j--)
if((numbers.get(j).distance(xCoord,yCoord))>0.3)
numbers.addElement(new Point2D.Double(xCoord,yCoord));
System.out.println("next element added : "+i+ " "+numbers.get(j)+" with distance: "
+(numbers.get(j).distance(xCoord,yCoord))+"from "+ xCoord + " "+ yCoord);
Iterator it = numbers.iterator ();
while (it.hasNext ())
System.out.println(it.next());
public static void main(String[] argv)
Distance d = new Distance();
d.getList();
}Thanks a lot -
I am trying to remove an element from a Vector and using the ff code:
Vector abc = getVector();
int index = 0;
abc.removeElementAt(index);
The element is not being removed, when i go back to check the number
of elements in the vector, it is still the same.
Please give me any ideas to make this work.Try this,
Vector abc = getVector();
int index = 0;
System.out.println("Size before : "+abc.size());
abc.remove(index);
System.out.println("Size after : "+abc.size());
What is the o/p?
Sudha -
Comparing elements in 2d array
Hi all
I need a solution. The problem is I have a 2-d array of numbers. I have to display a array subset such that the elements in the first row fall in the ranges provided by me through user prompt.04998285626 wrote:
...The problem is I have a 2-d array of numbers. I have to display a array subset such that the elements in the first row fall in the ranges provided by me through user prompt.
And...
04998285626 wrote:
its like i have a 2d array of numbers in increasing order. i input a number and i have to get the output array as all the elements that are less than it in the same order.( or rather get a subarray upon comparing the elements )
OK, let's try to interpret this once more in clearer language. Apparently, the first row has some significance.
Maybe only the first row is increasing in value and we want to keep the subset of the 2D containing all columns that are below a threshold in the first element. Since the values in the first row are increasing, all we need to do is find the column index where the first element is too big, and then take the subset of the 2D array up to this point. Here is a solution for this interpretation of the task.
You still might need to adjust for detail, e.g. use "greater or equal?" instead of "greater?" depending on your exact needs.
Message Edited by altenbach on 08-23-2008 11:19 AM
LabVIEW Champion . Do more with less code and in less time .
Attachments:
Trim2DArray.png 28 KB -
I am working on a small application in which a array keeps a list of character names. This list is used a parameter creating a drop down menu in a "JComboBox".
i my primary class (the one creating the different parts of the app) i create a Vecter that contains ManuCharacters.
The application function is, so the user can type in some names of some characters, that will be contained in this list, and so that they can modify this list be removing "ManuCharcters" again. In a textarea they should then be able to type in a manuscript and whenever the name of the character appears, words should be added to that character. So basically be able to count words for the different characters in a manuscript.
private Vector<ManuCharacter> charList = new Vector<ManuCharacter>();heres how the Vector is created...
private JComboBox CharacterList;
public somePanel(Vector<ManuCharacter> charList)
CharacterList = new JComboBox(charList);and heres how it is send to the JCombobox.
My question is now, is it possible to preserve the element in the vector at index 0, so that it cannot be deleted from the Vector? either by declaring it final or in some way?? The task is to make a ManuCharacter that will be at that exact element constantly, and that will get a count for words that the program cannot connect to a character.
My best solution is to use exception handling, some if statements and stuff, but if anyone knew a trick like "finalise" or other simple solution it would make it much easier.
If you need any more info to clarify my question, please let me know.One solution is to extend Vector class so that first element is unremovable/unchangable (or changable only if null/not set) etc.
class Vec<E> extends java.util.Vector<E> {
public E remove(int index) {
return index!=0 ? super.remove(index) : null;
And than use this new Vec class instead. -
Randomising order of elements in vectors
hi
i have a vector containing vectors containing Doubles (nightmare, i know!). Just wondering if there's an easy way of randomising the order or the elements in the main vector.
thanks,
alThis can be improved.
public class ARandouPermutation{
public static void main(String[] args) throws Exception{
java.util.Vector vec = new java.util.Vector();
for(int i=0;i<64;i++){// 64 is an example
vec.add(new Double(i*1.0));
Object[] tmp0 = vec.toArray();
int originalSize = vec.size();// obtaining the original size of the Vector
vec.clear(); // setting to size zero
Object[] tmp1 = new Object[originalSize];
int[] indexarr = randPermute(originalSize);// this method is defined below
for(int j=0;j<indexarr.length;j++){
tmp1[j] = tmp0[indexarr[j]];
tmp0=null;
arr=null;
for(int j=0;j<originalSize;j++){// this can be replaced by java.util.Arrays.asList(Object[])
vec.add(tmp1[j]);
System.out.println(((Double)vec.get(j)).toString());//test output
public static int[] randPermute(int L){// length L of the array returned is passed as an argument
if(L<0) throw new IllegalArgumentException();
int[] arr = new int[L];
java.util.Random rand = new java.util.Random();
for(int j=0;j<L;j++) arr[j]=j;
int tmp, index=0;
Label: while(index<L){
tmp = rand.nextInt(L);
for(int j=0;j<index;j++) {
if(tmp==arr[j]) continue Label;
arr[index]=tmp;
index++;
return arr;
} -
Hi,
I have got an vector called resultSet which contains String objects as elements.
eg.
NAME_FORMAT_TYPE
BON_PERF_REL
CER_FIGURE
CER_PERF
CER_QUOTE
CUR_PERF
CUR_QUOTE
VALUE_FORMAT_TYPE
+T2PC
T2
+T2C
T2
+T4C
T4
T2P
T2P
DESCRIPTION_FORMAT_TYPE
Format: �#.##0,00% Colored
Format: -#.##0,00
Format: �#.##0,00 Colored
Format: -#.##0,00
Format: �#.##0,0000 Colored
Format: -#.##0,0000
Format: -#.##0,00%
Now I want to put all the elements after NAME_FORMAT_TYPE to be put in a Vector called name and like wise all the elements after VALUE_FORMAT_TYPE
to be put in a vector called value and so on.
How can I do it ?
Thanksshubham
i made this logic, i havent compiled or checked the code so forgive any typos or syntax errors
Vector nameV=new Vector();
Vector valueV=new Vector();
Vector descV=new Vector();
int addTo=0;
for (int i=0;i<resultSet.size();i++){
if(resultSet.elementAt(i).toString().equals("NAME_FORMAT_TYPE")){
addTo=1;
continue;
if(resultSet.elementAt(i).toString().equals("VALUE_FORMAT_TYPE")){
addTo=2;
continue;
if(resultSet.elementAt(i).toString().equals("DESCRIPTION_FORMAT_TYPE")){
addTo=3;
continue;
switch (addTo) {
case 1:
nameV.addElement(resultSet.elementAt(i));
break;
case 2:
valueV.addElement(resultSet.elementAt(i));
break;
case 3:
descV.addElement(resultSet.elementAt(i));
break;
Another way to do the same wud be ...
int i1=resultSet.indexOf("NAME_FORMAT_TYPE");
int i2=resultSet.indexOf("VALUE_FORMAT_TYPE");
int i3=resultSet.indexOf("DESCRIPTION_FORMAT_TYPE");
int i=0;
for(i=1;i<i2;i++)
nameV.addElement(resultSet.elementAt(i));
for(i=i2+1;i<i3;i++)
valueV.addElement(resultSet.elementAt(i));
for(i=i3+1;i<resultSet.size();i++)
descV.addElement(resultSet.elementAt(i));
i hope i understood your problem correctly hope u can compile and check the outcome
best wishes & regds -
Extraction elements from vector
Hi everybody,
Can anybody help with this problem, pls Im a begginer with labview I would like solve a problem: There is matrix (2048x1 elements) and I would draw it point by point in wafeform chart, I have already tried everything, but I have allways drawn the whole vector.
Sorry for so stupid question......
best regards
thx tomasNot stupid, but it is resolved.
http://forums.ni.com/t5/LabVIEW/Graph-an-Array-but-not-every-row-of-it/td-p/1188601
Mahu -
Problems with inserting elements into vectors
Hi,
I have a problem with the setElementAt() method for vectors.
I want to initialise a vector that is same size as a previous vector declared in my program and insert float 0.0 in all the positions.
The code below is a for loop to do just this where count is the size of the previous vector.
for(int i=0;i<count; i++)
vLargest.setElementAt(zero,i);
}i have declared zero as a float
float zero = 0.0f;
the error that i'm getting is that it that the vector cannot be applied to a float.
Can anybody tell me what i'm doing wrong?
ThanksYes you need to store Objects in your Vector not primitives.
float is not an Object it is a primitive. But Float is an object. -
Compare elements of two or more clusters
Hello,
I am very new to labview and was hoping someone could help me with this. I am taking user input (motor #, size of steps, number of steps and sequence #) and storing all the data (for each motor) in a cluster.
(Now this is where I need help..)
I want to take the 4th element of each cluster (the sequence #) and sort it in order (1,2,3,4...). Based on the sequence number I want to use a flat sequence structure to send the commands to my motor controller in the right order.
Any help in solving this problem would be appreciated,
Regards,
MriduThe reason Crossrulz said to put the Sequence # first in your Cluster is because sorting starts with the first element, then the second, etc. If, however, you require that the Sequence number be, say, the third element, you can still sort it by using a trick (only do this if the array of clusters isn't huge) -- create a temporary cluster whose first element is the Sequence # and whose remaining element is the entire cluster. An array of these things will sort on the first element. Once sorted, take the Cluster apart and return only the second element, your original Cluster, now sorted. [I hope that was clear ...]
Bob Schor -
class Helloworld{
public static void main(String args[]){
B t1 = new B();
B t2 = new B();
B t3 = new B();
t1.start();
t2.start();
t3.start();
class A extends Thread{
static Vector v;
v = new Vector(0,1);
public void packMes(int d, int[] sData, int so, int[] dData, int doa, int size){
Message m = new Message(d, sData, so, dData, doa, size) //Message is a data class
addMessage(m);
public static synchronized void addMessage(Message pm){
v.addElement(pm);
class B extends A{
int[] intArray1 = {1,2,3,4};
int[] intArray2 = {11,12,13,14};
public void run(){
if (id==0) { // let id be the name of the current thread i.e "0" means thread-0
packMes(3, intArray1, 0, intArray2, 0, 4);
System.out.println("I'm thread "+id+" size: " + v.capacity());
Message test = (Message) v.elementAt(0);
}I tried to repeat running the program several times. The capacity of the vector in thread-0 is 1 all the time. Unfortunately sometimes it says
I'm thread 0 size: 1
java.lang.ArrayIndexOutOfBoundsException: 0 >= 0
at java.util.v.elementAt(Unknown Source)
at helloWorld.run(B.java:21)
[i]
Please help. Thanks in advance.here the exact code.
type
java Helloworld 4 B
after compiled.
The same problem still exists although it doesn't happen all the time.
import java.io.*;
import java.util.*;
public class Helloworld{
static int processor; // total number of processors
static String fileName;
static int counter; // counter for processors
public static void main(String args[]){
// interface with users
processor = Integer.parseInt(args[0]);
fileName = args[1];
try{
Class c = Class.forName(fileName);
Thread procs[] = new Thread[processor]; // declare an array for threads
for (int i=0; i<processor; i++){
System.out.println(i); // create threads
procs[i] = (Thread) c.newInstance();
procs.start();
} catch (ClassCastException ex)
{ System.out.println("The file you provided is not a thread"); }
catch (ClassNotFoundException ex)
{ System.out.println("That class doesn't exist"); }
catch (Exception ex)
{ System.out.println("File error!"); }
class A extends Thread{
static Vector v;
public void createVector(){
v = new Vector(0,1);
public void packMes(int desId, int[] sourData, int sourOffset, int[] destData, int destOffset, int size) {
Message m = new Message(desId, sourData, sourOffset, destData, destOffset, size);
addMessage(m);
public int id(){
String procsName = getName();
int procsID=Integer.parseInt(procsName.substring(7));
return procsID;
public static void addMessage(Message pm){
v.addElement(pm);
class B extends A{
int[] intArray1 = {1,2,3,4};
int[] intArray2 = {11,12,13,14};
public void run(){
createVector();
if (id()==0) {
packMes(3, intArray1, 0, intArray2, 0, 4);
System.out.println("I'm thread "+id()+" size: " + v.size());
Message test = (Message) v.elementAt(0);
class Message {
int destID;
int size;
public Message(int destID, long[] sourData, int sourOffset, long[] destData, int destOffset, int size) {
this.destID = destID;
long[] sData = sourData;
int sOffset = sourOffset;
long[] dData = destData;
int dOffset = destOffset;
this.size = size;
public Message(int destID, int[] sourData, int sourOffset, int[] destData, int destOffset, int size) {
this.destID = destID;
int[] sData = sourData;
int sOffset = sourOffset;
int[] dData = destData;
int dOffset = destOffset;
this.size = size; -
Need Help : compare element in list
Hi all,
I have a list of set : [[abc] [ac] [abc] [a]]
I want to compare a set with each other. For example:[abc] with [ac] = [ac] // result is [ac] and newSet will hold the result
[ac] with [ac] = [ac]
[ac] with [abc] = [ac]
[ac] with = [ac][b]
[ac][b] with [a] = [c][b]
My problem is how to hold the result to compare another set.
Below is my code…I’m stuck here for one day…please show me what should I do..
public static void intersect(List listSet2) { // listSet2 ; [[abc] [ac] [abc] [a]]
TreeSet set = new TreeSet(((Set)listSet2.get(0)));
for (int i=1; i < listSet2.size(); i++) {
TreeSet newSet = new TreeSet();
Iterator it = ((Set) listSet2.get(i)).iterator();
while (it.hasNext()){
Object o = it.next();
if(set.contains( o ) ){// I want set hold the latest set
newSet.add( o );
System.out.println("Intersection : " + newSet);Hi..
I had settle my first problem to hold the latest result but I have the problem when the result find to next set which contains different value...it will return [ ]...actually I want it do union if find different value...I have no idea to settle it....can anyone give me some idea....tq very much
[abc] with [ac] = [ac]
will hold the result
[ac] with [ac] = [ac]
[ac] with [abc] = [ac]three above are settle
[ac] with = [ac][b]
it return [ ]
[ac] with [a] = [c][b]
Below is my code : public static void intersect(List listSet2) {
Set inter = new HashSet((Set)listSet2.get(0));
Set union = new HashSet();
for (int i=1; i < listSet2.size(); i++){
inter.retainAll((Set)listSet2.get(i));
System.out.println("Inter " + inter); -
Comparing elements of array to input
If I read in a letter, or word from the user, how would i be able to compare it to see if it is equal to an item in the array.
ex. In the first slot of the array I have HELLO, and now the user types HELLO, how would I compare it to that item in the array?
thanksif (array[0].equals(stringInputByUser))
Maybe you are looking for
-
Jtree, Double Click to open a file
Here is what I have so far: tree.addMouseListener(new MouseAdapter(){ public void mouseClicked(MouseEvent evt){ if (evt.getClickCount()==2){ TreePath tp = tree .getClosestPat
-
Can't run install for CR XI DEV WIN INTL from SAP download site - corrupt?
The file is .zip file download that will not install. It gives me an error: Windows cannot access the specified device, path, or file. You may not have the appropriate permissions to access the item. I'm running as an domain admin on a w2k3 server...
-
Suddenly can't connect to Yahoo email? "Incorrect username or password".
I thought I was past all the email headaches I've had with the X. Its certainly not a Blackberry...lol Everything has been working fine for the last month and now all of a sudden it wont connect to Yahoo. I've tried removing the account and setting i
-
hi all, I am developing a adobe form, i need to create a dynamic length table. I have created the tabled and done the required settings also, but the table doesnt flow to the next page. i am listing he things i did, may be somebody can tell me if i h
-
Time Machine ejects my Ext Drive
Hi, I hooked up an external (1.5TB) drive to my MacBook and ran time machine. It said backing up 103GB. Everything went fine until about 14GB and somehow ejected my ext drive. The drive disappeared. I unhooked and put the drive back. Ran TM again. Th