This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Boolean method for a class with an array...Please help!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Boolean method for a class with an array...Please help!!" Watch "Boolean method for a class with an array...Please help!!" New topic

Boolean method for a class with an array...Please help!!

celine scarlett
Ranch Hand

Joined: Nov 06, 2005
Posts: 93

I'm still new to Java, and get a bit lost sometimes implementing simple stuff.

I am writing a class that defines an array, and needs to have two constructors, one that takes no parameters and creates an empty list, and another that takes the name of a text file as a parameter and creates a list containing the numbers listed in the text file.

I have also added a method that adds a new number to the list at any time. However, I need a boolean method that returns 'true' if the list contains the given number and 'false' otherwise. The problem is, I'm not sure how to implement this correctly.

Also, how do I construct a toString method that will return a string representing the numbers in the list.

My code so far is as follows:

package testing;

import simplejava.SimpleReader;

public class NumberList {

int [] numbers;
int arraySize, size;

public NumberList()
int[] numbers = new int[10000];
arraySize = 10000;
size = 0;

public NumberList(String fileName)
{ SimpleReader file = new SimpleReader(fileName);
size =0;
int next = file.readInt();
while (!file.finished())
{ size++;
next = file.readInt();

file = new SimpleReader(fileName);
numbers = new int[size];
arraySize = size;
for (int i = 0; i<size; i++)
numbers[i] = file.readInt();

public void add(int next)
numbers[size] = next;


Any help would be appreciated!!!
Joanne Neal

Joined: Aug 05, 2005
Posts: 3742
You'd be better using an ArrayList rather than an array. These can grow dynamically and there are methods for adding to the end or at a specific point, so you wouldn't need to maintain a reference to the next insertion point, you wouldn't need to read your file twice and you wouldn't need to allocate such a large array to start with.
Take at a look at the javadoc and come back if you're not sure of anything.

celine scarlett
Ranch Hand

Joined: Nov 06, 2005
Posts: 93

Thanks for the help. I shall have a look at that appraoch and get back to you if there are any further problems.

Many thanks!!
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
If you've never used collections, you should take the tutorial:

There is no emoticon for what I am feeling!
marc weber

Joined: Aug 31, 2004
Posts: 11343

First, note that the word "List" has a specific meaning in Java. It is an interface implemented by classes such as ArrayList and LinkedList. There is no List in this code (and whether that's a good choice is another issue), so to avoid confusion you might want to call this something else.

With respect to your boolean "contains" method, I'll suggest 2 approaches:
  • Use the java.util.Arrays class to sort and search your array. (See the API.)
  • Write your own code to iterate through the array and test each element for equality to the test value.
  • With respect to the toString method you describe, you basically need to iterate through the array and concatenate each element. The best way to do this (especially given the size of your array) is to use a StringBuffer or StringBuilder. (See the API.)

    (Please use CODE tags, and please don't cross-post.)
    [ November 10, 2005: Message edited by: marc weber ]

    "We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
    celine scarlett
    Ranch Hand

    Joined: Nov 06, 2005
    Posts: 93

    Thanks for all the advice. I'm sorry for cross-posting. Won't happen again!!

    Best Wishes.
    I agree. Here's the link:
    subject: Boolean method for a class with an array...Please help!!
    It's not a secret anymore!