IntList - If the list is full, // prints a message and does...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
// **************************************************************** // IntList.java // // An (unsorted) integer list class with a method to add an // integer to the list and a toString method that returns the contents // of the list with indices. // // **************************************************************** public class IntList { protected int[] list; protected int numElements = 0; //------------------------------------------------------------- // Constructor -- creates an integer list of a given size. //------------------------------------------------------------- public IntList(int size) { list = new int[size]; } //------------------------------------------------------------- // Adds an integer to the list.
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: If the list is full, // prints a message and does nothing. //------------------------------------------------------------- public void add(int value) { if (numElements == list.length) System.out.println("Can't add, list is full"); else { list[numElements] = value; numElements++; } } //------------------------------------------------------------- // Returns a string containing the elements of the list with their // indices. //------------------------------------------------------------- public String toString() { String returnString = ""; for (int i=0; i<numElements; i++) returnString += i + ": " + list[i] + "\n"; return returnString; } }...
View Full Document

This note was uploaded on 03/22/2010 for the course CS 202 taught by Professor Fong during the Spring '10 term at SIU Carbondale.

Ask a homework question - tutors are online