Jump to content
  • Advertisement
Sign in to follow this  
Daedalus AI

[java] A Vector problem

This topic is 4588 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

    private void runLineChart()
    {        
	while ( running == true )
	{	    
            try
            {
                Thread.sleep( 2000 );
            }
            catch ( InterruptedException theException )
            {
                theException.getMessage();
            }

            LineCoOrdinates.add( MyFileIO.getNextGraphPoint() );
            
            repaint();
	}
    }


MyFileIO.getNextGraphPoint() returns a string, which is currently always the same (i.e. LineCoOrdinates.add( "20" ); ) What I don't get is why my LineCoOrdinates vector is not incrementing, but is increasing in capacity. Basically, it stays as; { "20" } no matter how many times I add the same object to it; It should go; { "20" } { "20", "20" } { "20", "20", "20" } etc. I have also tried explicity specifying the index to add them in (i.e. LineCoOrdinates.add( Iter++, "20" ); )... same result. Any ideas anyone? Thanx, Daed

Share this post


Link to post
Share on other sites
Advertisement
Is it a java.util.Vector? If so, use addElement().

And why does it have to be a Vector? Doesn't a List (LinkedList) makes more sense for the current problem?

Son Of Cain

Share this post


Link to post
Share on other sites
I'm not that good with linked lists, plus I think that would be a bit of an overkill.
Also, addElement() is exactly the same as add()

Thanx, Daed.

Share this post


Link to post
Share on other sites
No dude, List and LinkedList are much simpler (and also faster) than Vector. Since you're dealing with graph points, it would make sense due to the LinkedList methods (peek(), pool(), etc...).

About the add() and addElement() stuff, I've always used addElement() to add an object to a Vector, so I thought that would help you, sorry =D

Son Of Cain

Share this post


Link to post
Share on other sites
According to the Collection interface API:
Quote:

public boolean add(Object o)

Ensures that this collection contains the specified element (optional operation). Returns true if this collection changed as a result of the call. (Returns false if this collection does not permit duplicates and already contains the specified element.)


I don't know the inner workings of Vector or whether or not it allows duplicates. I would check the return value of add() to see if that is the case.

Share this post


Link to post
Share on other sites
Try Lists
Quote:
from Java website:
Unlike sets, lists typically allow duplicate elements. More formally, lists typically allow pairs of elements e1 and e2 such that e1.equals(e2), and they typically allow multiple null elements if they allow null elements at all. It is not inconceivable that someone might wish to implement a list that prohibits duplicates, by throwing runtime exceptions when the user attempts to insert them, but we expect this usage to be rare.

Share this post


Link to post
Share on other sites
Are you sure you're not modifying this Vector somewhere else or accidentally clearing it? Do you have more code you can post?

Share this post


Link to post
Share on other sites
Quote:
Original post by Alpha_ProgDes
Try Lists
Quote:
from Java website:
Unlike sets, lists typically allow duplicate elements. More formally, lists typically allow pairs of elements e1 and e2 such that e1.equals(e2), and they typically allow multiple null elements if they allow null elements at all. It is not inconceivable that someone might wish to implement a list that prohibits duplicates, by throwing runtime exceptions when the user attempts to insert them, but we expect this usage to be rare.


Yeah, I saw that before and thought "Great", but still the same.

I've changed it now to a way that works (although it does the same thing).

This was the function that returned the string, I now add the elements to a vector immediately in this function, then return the vector.


public Vector getNextGraphPoint( Integer theLoopNumber )
{
String StringIn = "0";

try
{
FileIn = new BufferedReader( new FileReader( FileName ) );

GraphPoints.removeAllElements();

while ( ( ( StringIn = FileIn.readLine() ) != null ) && ( theLoopNumber >= 0 ) )
{
System.out.println( StringIn );
GraphPoints.add( StringIn );
--theLoopNumber;
}

FileIn.close();

return GraphPoints;
}
catch ( IOException TheException )
{
System.out.println( "Failed to read from file: " + FileName );
}

return GraphPoints;
}



This is not the first time I've had this problem with vector's, and I cannot work out for the life of me what is actually happening behind the scenes.

Daed.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!