aspose file tools*
The moose likes Beginning Java and the fly likes For loop through a vector not allowing me to use .length? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "For loop through a vector not allowing me to use .length?" Watch "For loop through a vector not allowing me to use .length?" New topic
Author

For loop through a vector not allowing me to use .length?

Dave Allen UK
Greenhorn

Joined: Mar 11, 2009
Posts: 2
Hi All

I am currently writing a Scrabble program and working on the bag that contains all of the tiles. I have set up the tiles using a vector that stores their frequency. I am trying to iterate through the vector using a for loop but for some reason I keep getting the following error,

This happens everytime I try and iterate through the vector which is a few times. My code for the whole things is as follows,

I should mention that this is for my uni coursework and we are not allowed to use calsses from the java.util package, I was wondering if that is the reason it does not work.

Any help is very very much appreciated. Thanks very much.
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30753
    
156

Where do you define length in MyVector? Is it a method or a field?


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Dave Allen UK
Greenhorn

Joined: Mar 11, 2009
Posts: 2
Well I thought that the .length method returned how many elements there were in the vector. I have also tried tilelist.size() but that does not seem to work either. Not sure if that answers your question though, do I need to have a method in the MyVector that adds up how many there are?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18651
    
    8

If you're asking about that MyVector class you posted up there, then you can look at the code yourself and see what methods it has. You can see that it doesn't have any size() method just by looking at it. (There are two methods, but neither of them are called size().) Likewise you can see that it doesn't have a length variable. (It has two variables but neither are called length.)

But wait! This MyVector class might be a subclass of something which has one of those things! But no... it's only a subclass of Object, which doesn't have a size() method or a length variable.

So I don't know where you expected those methods to come from. Is that your class? Then it's up to you to write them. They won't appear by magic just because you try to use them.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39395
    
  28
Welcome to the Ranch

You would find it easier to set up the tiles with chars in your for loop. You mightn't believe it, but you can alter it toHave you been told you use Vector rather than the more efficient ArrayList? Do you know how to put the letters, counts and tile values into a Map?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: For loop through a vector not allowing me to use .length?