Clear Vector list

ZURINET
ZURINET used Ask the Experts™
on
Hi all

I have the attached code..
For every single interation of method  <<public void one_iteration(int t) >>
a value of g is added to besFit Vector :::  bestFit.add(g);
I need a way of clearing all the assingment to bestFit, before the next Interaton occours... given
the fact that there could be up to 50 interations..

Thanks in Advance      
Given that a sample g = (c1	0	0	1) is a vector
*****************************************************
c1	0	0	1
c2	0	1	0
c3	1	0	0
*****************************************************
public void one_iteration(int t){
	CellNode bestFit;  //Class CellNode (0,0,0)
	CellNode tempNode;	
	double [] wUpdate;
	double varInflue;
	double [] varW_Vector;
	
	Gene[] random_order = shuffle();
	
	for(int j=0; j<8.length; j++){
		Gene g = random_order[j]; //Given than g = ()
		
		bestFit =   find_best_Fit(g); 
        bestFit.add(g);  //Given that bestFit has a method clear_list : which clears all the g containt of bestFit
		
		for(int i=0; i<6; i++)
		{
					int xBMap = map[i].get_x();
					int yBMap = map[i].get_y();
					currentNode = new CellNode(xBMap,yBMap, xBMap);
				
				varW_Vector = map[i].get_weight_vector();
				wUpdate = Function.update_weight_vector(varInflue, varW_Vector,currentNode);
				
				if (varInflue > 0)
				{	
					map[i].set_weight_vector(wUpdate);
				}
		}
	}
}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2016

Commented:
>>I need a way of clearing all the assingment to bestFit

Why - what happens when you don't?
Awarded 2011
Awarded 2011

Commented:


That's how you declare bestFit:
CellNode bestFit

Is CellNode the class, which you defined?
Show the code which defines this class.
You need to define method clear_list() in this class yourself.
Or you can create new instance of it.
Top Expert 2016
Commented:
So far, i'm unclear as to why and what you want to do, but in general, to 'clear' a double[]:
Arrays.fill(array, 0.0);

Open in new window

CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

Awarded 2011
Awarded 2011

Commented:
If your bestFit would indeed be an instance of a Vector or ArrayList
class or would extend this class,
then in both of these classes there is a method

clear(),

so you'd remove all elements of this container with

bestFit.clear();

Awarded 2011
Awarded 2011

Commented:

As CellNode is something that supposedly you wrote yourself.
You probably provided method add, as we see in your code,

 bestFit.add(g);  

So you should also provide method clear(),
which would remove all your elements.



Awarded 2011
Awarded 2011
Commented:
As one can try to guess from your code,
you probably meant to have bestFit as a collection of CellNode elements,

Then you should have rather declared:

ArrayList bestFit = new ArrayList();

then you can populate your array list, either directly:

CellNode currentNode = new CellNode(xBMap,yBMap, xBMap);
bestFit.add(currentNode);

or maybe retrive it from some other method
where it was poopulated
bestFit =   find_best_Fit(g);

then you would be able at appropriate place to use clear() method and remove
all elemnts of your ArrayList to start collecting new elements

What we see in your current code above seems strange,
as on the one hand your CellNode class should have a method add(..),
so it looks like it is some sort of collection,
On the other hand, we see

currentNode = new CellNode(xBMap,yBMap, xBMap);

which shows that CellNode is a single node, does not look like collection.
Besides currentNode is not declared before you start
creating it.

Please, look at your code attentively, and correct it, so that it becomes
more obvious what you mean.

The good practice in fact is to write code in IDE, which will usually
highlight the incompilable parts of it just as you type.




Author

Commented:
Hi all

The problem is that
bestFit =   find_best_Fit(g);
is used as a reference ... to indicate which vector to work with..
Hence after working with the vector ... I need to release the the g..
If g is not released the the value will be double during the next interation..

Author

Commented:
Hi
Yes CellNode is a class,

Awarded 2011
Awarded 2011

Commented:
But is bestFit an instance of CellNode or it is rather an instance of a vector, arraylist, basically is it a collection ?

Author

Commented:
bestFit an instance of CellNode and CellNode is a collection
Awarded 2011
Awarded 2011

Commented:
But you have a constructor like that:

 new CellNode(xBMap,yBMap, xBMap)

Do you have the code for CellNode?
Post the code.
Awarded 2011
Awarded 2011

Commented:
In the CellNode code we should have both the constructor
which has three integeres like in
 new CellNode(xBMap,yBMap, xBMap) (do you really need to have xbMap twice here?)
and it should have method
.add(..) as you have in bestFit.add

Please, look attentively at your code and consider which
variables belong to which types.

It is best of all to type any Java code in the IDE - it would help
to keep code organized in terms of types and classes, and it would highlight all
undeclared varaibles or incompatible variable types, non-existing methods.
It very much helps to think when you are designing even small programs

Author

Commented:
Hi all

It quite clear to me that the question definition is not quite clear..
However.. after reading your notes.

and my comment <<I need a way of clearing all the assingment to bestFit, before the next Interaton occours... >

I need to do clear before the loop is called  <<Gene[] random_order = shuffle();>>
Which I did with the code below..

Any way thanks for your input

Regards

      
 
for(int i=0; i<bestFit.length; i++)
{
bestFit.clear_list  //give that bestFit has a method clear_list 
}

Open in new window

Author

Commented:
Question was resolved based on the idea.. received from experts..

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial