Results 1 to 4 of 4
Thread: Removing Method
- 11-09-2010, 12:12 AM #1
Senior Member
- Join Date
- Oct 2010
- Location
- Newark,nj
- Posts
- 111
- Rep Power
- 0
Removing Method
Java Code://******************************************************************** // CDCollection.java Author: Lewis/Loftus // // Represents a collection of compact discs. //******************************************************************** import java.text.NumberFormat; public class CDCollection { private CD[] collection; private int count; private double totalCost; private CD[] temp; //----------------------------------------------------------------- // Constructor: Creates an initially empty collection. //----------------------------------------------------------------- public CDCollection () { collection = new CD[100]; count = 0; totalCost = 0.0; [B] }///start method public String removeCd(String art) { int i =0; while (!collection[i].equals(art)){ i++; } int j = 0; temp= new CD[collection.length-1]; for ( int j = 0; j< collection.length-1; j++) { collection[j]=temp[j]) return System.out.println(collection[j]); [/B] } }// Trying to make a remove method that takes deletes a cd and shifts the space(The part in bold)...
- 11-09-2010, 01:10 AM #2
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
Unless the array is some sort of requirement, if you're doing something that has a lot of add, remove and lookup commands, I'd suggest using one of the List implementations, ArrayList for example. Look it up.
Ever seen a dog chase its tail? Now that's an infinite loop.
- 11-09-2010, 02:35 AM #3
Senior Member
- Join Date
- Oct 2010
- Location
- Newark,nj
- Posts
- 111
- Rep Power
- 0
Can anyone tell me why im getting a null pointer exception plzz b4 12 a.m..
//-----------------------------------------------------------------Java Code://******************************************************************** // CDCollection.java Author: Lewis/Loftus // // Represents a collection of compact discs. //******************************************************************** import java.text.NumberFormat; public class CDCollection { private CD[] collection; private int count; private double totalCost; private CD[] temp; //----------------------------------------------------------------- // Constructor: Creates an initially empty collection. //----------------------------------------------------------------- public CDCollection () { collection = new CD[100]; count = 0; totalCost = 0.0; }///s[B]tart method public void removeCD(String title) { int i =0; while (!collection[i].equals(title)); { i++; for (int g=i; g<collection.length-1;g++){ if (g==collection.length-1) { collection[collection.length -1] =null; } else { collection[g] = collection[g+1]; } } int j = 0; temp= new CD[collection.length-1]; for ( j = 0; j< collection.length-1; j++) { collection[j]=temp[j] ; } } } [/B] //----------------------------------------------------------------- // Adds a CD to the collection, increasing the size of the // collection if necessary. //----------------------------------------------------------------- public void addCD (String title, String artist, double cost, int tracks) { if (count == collection.length) increaseSize(); collection[count] = new CD (title, artist, cost, tracks); totalCost += cost; count++; } [CODE]//******************************************************************** // Tunes.java Author: Lewis/Loftus // // Demonstrates the use of an array of objects. //******************************************************************** public class Tunes { //----------------------------------------------------------------- // Creates a CDCollection object and adds some CDs to it. Prints // reports on the status of the collection. //----------------------------------------------------------------- public static void main (String[] args) { CDCollection music = new CDCollection (); music.addCD ("Storm Front", "Billy Joel", 14.95, 10); music.addCD ("Come On Over", "Shania Twain", 14.95, 16); music.addCD ("Soundtrack", "Les Miserables", 17.95, 33); music.addCD ("Graceland", "Paul Simon", 13.90, 11); music.removeCD("Double live"); System.out.println (music); music.addCD ("Double Live", "Garth Brooks", 19.99, 26); music.addCD ("Greatest Hits", "Jimmy Buffet", 15.95, 13); System.out.println (music); } }
// Returns a report describing the CD collection.
//-----------------------------------------------------------------
public String toString()
{
NumberFormat fmt = NumberFormat.getCurrencyInstance();
String report = "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n ";
report += "My CD Collection\n\n";
report += "Number of CDs: " + count + "\n";
report += "Total cost: " + fmt.format(totalCost) + "\n";
report += "Average cost: " + fmt.format(totalCost/count);
report += "\n\nCD List:\n\n";
for (int cd = 0; cd < count; cd++)
report += collection[cd].toString() + "\n";
return report;
}
//-----------------------------------------------------------------
// Increases the capacity of the collection by creating a
// larger array and copying the existing collection into it.
//-----------------------------------------------------------------
private void increaseSize ()
{
CD[] temp = new CD[collection.length * 2];
for (int cd = 0; cd < collection.length; cd++)
temp[cd] = collection[cd];
collection = temp;
}
}
[/CODE]
- 11-09-2010, 04:07 AM #4
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
Your temp array is one element shorter than the collection array, so the last iteration of the for loop spawns the exception.Java Code:temp= new CD[collection.length-1]; for ( j = 0; j< collection.length-1; j++) { collection[j]=temp[j] ; } } }
Also, did you think about my suggestion on using a List?Ever seen a dog chase its tail? Now that's an infinite loop.
Similar Threads
-
Removing a JButton
By Lingerz in forum New To JavaReplies: 15Last Post: 01-10-2010, 12:18 AM -
Removing Duplicates.
By dashwall in forum New To JavaReplies: 9Last Post: 12-29-2009, 01:03 PM -
Removing Indexes
By gilbertsavier in forum JDBCReplies: 0Last Post: 07-17-2009, 07:23 AM -
trouble in removing a value
By jacline in forum New To JavaReplies: 5Last Post: 03-20-2009, 05:56 PM -
removing reference
By ajith_raj in forum Advanced JavaReplies: 4Last Post: 02-12-2009, 11:46 AM


LinkBack URL
About LinkBacks

Bookmarks