4 Replies - 4790 Views - Last Post: 01 January 2009 - 11:14 AM Rate Topic: -----

#1 Brandon222  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 99
  • Joined: 16-November 08

exapanding and add something to an array

Posted 02 December 2008 - 07:51 PM

import java.util.Scanner;
import java.io.*;

class Driver {
  public static void main(String[]args) throws IOException{
	
	CDCollection music = new CDCollection();
	
	String nameEntered;
	boolean runChoices = true;
	int input = 0;
	String name; 
	String singer;
	double cost;
	int numTracks;
	Scanner sc = new Scanner(System.in);
	
	System.out.println("******************************************");
	System.out.println("*Hello and Welcome to Your CD Collection.*");
	System.out.println("******************************************");  
	
	while (runChoices = true){
	music.menu();
	
	input = sc.nextInt();
		if (input == 1){
		  
		  System.out.println("Enter title of song.");
		  name = sc.nextLine();
		  System.out.println("Enter the artist name.");
		  singer = sc.nextLine(); 
		  System.out.println("Enter the cost of CD.");
		  cost = sc.nextInt();
		  System.out.println("Enter the number of tracks on the CD.");
		  numTracks = sc.nextInt();
		  music.addCD(name, singer, cost, numTracks);
		}
		else if (input == 2){
		  
		  
		  //makes deposite and show amount
		  
		}
		
		else if (input == 3){
		  
		  music.showCds();
		  } 
		  
		 else {
		  
		  runChoices = false;
		  
		  System.out.println("***************************************");
		  System.out.println("*Thank You For Using MyMusic, GoodBye!*");
		  System.out.println("***************************************");
		  
		}
	  
	

	
	
  }//Main
  
}
}//Driver


import java.util.Scanner;
import java.io.*;

public class CDCollection {
  private double totalValue;
  int currentsize = 30;
  private CD [] collection = new CD [ currentsize ]; 
  int count = 0;
  
  public CDCollection () throws IOException{
	
	Scanner textIn = new Scanner (new File ("CDs.txt"));
	int i = 0;
	while (textIn.hasNext()){
	  String line = textIn.nextLine();
	  String [] details = line.split("," , 4);
	   collection [i] = new CD (details [0] , details [1], Double.parseDouble (details [2]), Integer.parseInt (details [3]));
	  i++;
	  
	}
  }//IO Exception CDCollection method

  public void showCds(){
	for (int i = 0; i < collection.length; i++){
	System.out.println(collection[i].toString());
	int a = i;
  }
}

  public void menu(){
	
	System.out.println("Please enter one of the following numbers.");
	System.out.println("1. Add new CD.");
	System.out.println("2. Sort Collection.");
	System.out.println("3. Display Collection.");
	System.out.println("4. Exit the System.");
	
	
  }//menu method

  public void addCD (String title, String artist, double value, int tracks){
	
	for (int i = 0; i < collection.length; i++){
	  if (collection [i]. equals ("")){
		collection[i] = new CD (title, artist, value, tracks);
	  }
	  
	}
	System.out.println("");
		for (int i = 0; i < collection.length; i++){
	System.out.println(collection[i].toString());
		}
		
}
  public void increaseSize(){
  private CD [] temp = new CD [ currentsize * 2 ];
}
}



public class CD{
  private String title, artist;
  private double value;
  private int tracks;
  
  public CD (String title, String artist, double value, int tracks){
	this.title = title;
	this.artist = artist;
	this.value = value;
	this.tracks = tracks;
	
  }
  
  public String toString(){
	return title + " | " + artist + " | " + value + " | " + tracks;
	
  }
  
}


ok i have to make a program that wear it asks the user if it wants to see all the cds add a new cd or sort the cds. My problem is that i dont kno how to add something to an array and also if the array is full how i expand it. All i know is that you have to make a new array and copy everything from the old array into there. Could someone please teach me how to do that. I would be most greatful.

Is This A Good Question/Topic? 0
  • +

Replies To: exapanding and add something to an array

#2 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1184
  • View blog
  • Posts: 7,254
  • Joined: 07-September 06

Re: exapanding and add something to an array

Posted 02 December 2008 - 08:32 PM

Expandable arrays are only possible with a List
Look into ArrayList, it acts like an array without having any specific size requirements.

It has easy methods like [ArrayList]<TYPE>.add([Value]);. It also has a coexisting library that allows it to do all the nice sorting stuff for you.

HTH
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: exapanding and add something to an array

Posted 02 December 2008 - 09:18 PM

View PostBetaWar, on 2 Dec, 2008 - 07:32 PM, said:

Expandable arrays are only possible with a List
Look into ArrayList, it acts like an array without having any specific size requirements.

It has easy methods like [ArrayList]<TYPE>.add([Value]);. It also has a coexisting library that allows it to do all the nice sorting stuff for you.

HTH

School assignment.... probably not authorized to use ArrayList or Vector

method to increase the size of a CD by 10


if(myCDarray is full) {
	myCdarray = add10(myCDarray);
}
....


CD[] add10(CD[] array) {
		CD[] nArray = new CD[array.length + 10);
		for(int i = 0; i < array.length; i++)
		   nArray[i] = array[i];
		return nArray;
}


Was This Post Helpful? 1
  • +
  • -

#4 Brandon222  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 99
  • Joined: 16-November 08

Re: exapanding and add something to an array

Posted 03 December 2008 - 02:00 PM

View Postpbl, on 2 Dec, 2008 - 08:18 PM, said:

View PostBetaWar, on 2 Dec, 2008 - 07:32 PM, said:

Expandable arrays are only possible with a List
Look into ArrayList, it acts like an array without having any specific size requirements.

It has easy methods like [ArrayList]<TYPE>.add([Value]);. It also has a coexisting library that allows it to do all the nice sorting stuff for you.

HTH

School assignment.... probably not authorized to use ArrayList or Vector

method to increase the size of a CD by 10


if(myCDarray is full) {
	myCdarray = add10(myCDarray);
}
....


CD[] add10(CD[] array) {
		CD[] nArray = new CD[array.length + 10);
		for(int i = 0; i < array.length; i++)
		   nArray[i] = array[i];
		return nArray;
}



Thanks this help me understand it better, however i changed it and made it different. I just have one more question. My code now works. I can add a song and expand my array if it is full. My problem is that when i add a new cd, it ereases the first song on the list and puts it in its place. Could someone please tell me how to fix this. Here is my code

public class CD{
  private String title, artist;
  private double value;
  private int tracks;
  
  public CD (String title, String artist, double value, int tracks){
	this.title = title;
	this.artist = artist;
	this.value = value;
	this.tracks = tracks;
	
  }
  
  public String toString(){
	return title + " | " + artist + " | " + value + " | " + tracks;
	
  }
  public String getTitle(){
	return title;
  }
}


import java.util.Scanner;
import java.io.*;

public class CDCollection {
  private double totalValue;
  int currentSize = 30;
  private CD [] collection = new CD [ currentSize ]; 
  int count = 0;
  int position;
  
  public CDCollection () throws IOException{
	
	Scanner textIn = new Scanner (new File ("CDs.txt"));
	int i = 0;
	while (textIn.hasNext()){
	  String line = textIn.nextLine();
	  String [] details = line.split("," , 4);
	  collection [i] = new CD (details [0] , details [1], Double.parseDouble (details [2]), Integer.parseInt (details [3]));
	  i++;
	  
	}
  }//IO Exception CDCollection method
  
  public void showCds(){
	for (int i = 0; i < collection.length; i++){
	  System.out.println((i+1) + " " + collection[i].toString());
	}
  }
  
  public void menu(){
	
	System.out.println("Please enter one of the following numbers.");
	System.out.println("1. Display Collection.");
	System.out.println("2. Add a CD.");
	System.out.println("3. Delete a CD.");
	System.out.println("4. Sort CDs.");
	System.out.println("5. Exit the System.");
	
	
  }//menu method
  
  public void addCD (String title, String artist, double value, int tracks){
	
	if (count == currentSize){
	  increaseSize();
	}
	collection [count] = new CD (title, artist, value, tracks);
	count += 1;
	
  }
  public void increaseSize(){
	
	currentSize = currentSize * 2;
	
	CD [] temp = new CD [currentSize];
	
	for (int i = 0; i < collection.length; i++){
	  temp [i] = collection [i]; 
	}
	collection = temp;
  }
  public void deleteTheCD(String name){
	
	boolean found = false;
	int i = 0;
	while (!found){
	  if (name.equalsIgnoreCase(collection[i].getTitle())){
	  position = i;
		while (position < (count - 1)){
		  collection[position] = collection[position + 1];
		  position++;
		}
		collection [count - 1] = null;
		System.out.println("Your CD has been succesfully deleted");
		  found = true;

	  }
	 i++;
	}
  count--;
  }
}



import java.util.Scanner;
import java.io.*;

class Driver {
  public static void main(String[]args) throws IOException{
	
	CDCollection music = new CDCollection();
	
	String nameEntered;
	boolean runChoices = true;
	int input = 0;
	String name; 
	String singer;
	double cost;
	int numTracks;
	String deleteSong;
	Scanner sc = new Scanner(System.in);
	Scanner text = new Scanner(System.in);
	
	System.out.println("********************************************");
	System.out.println("* Hello and Welcome to Your CD Collection. *");
	System.out.println("********************************************");  
	
	while (runChoices == true){
	  music.menu();
	  
	  input = sc.nextInt();
	  if (input == 1){
		
		music.showCds();
	  
	  }
	  else if (input == 2){
		
		System.out.print("Enter title of song:");
		name = text.nextLine();
		System.out.print("Enter the artist name:");
		singer = text.nextLine(); 
		System.out.print("Enter the cost of CD:");
		cost = sc.nextDouble();
		System.out.print("Enter the number of tracks on the CD:");
		numTracks = sc.nextInt();
		music.addCD(name, singer, cost, numTracks);
		
	  }
	  
	  else if (input == 3){
		System.out.println ("Enter the title of the song you wish to delete.");
		deleteSong = sc.next();
		music.deleteTheCD(deleteSong);
	  } 
	  
	  else {
		
		runChoices = false;
		
		System.out.println("*****************************************");
		System.out.println("* Thank You For Using MyMusic, GoodBye! *");
		System.out.println("*****************************************");
		
	  }
	  
	  
	  
	  
	  
	}//Main
	
  }
}//Driver


Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10781
  • View blog
  • Posts: 40,152
  • Joined: 27-December 08

Re: exapanding and add something to an array

Posted 01 January 2009 - 11:14 AM

Try using ArrayList (Resizable Array) or LinkedList (Uses Pointers not an Array). They both work about the same for the purpose of this project. Each has an add() method, set() method, get() method, remove() method and clear() method. Add() inserts an item into the list, and you can specify an index but don't have to. If you choose to specify the index, it moves the existing elements to the next index, keeping data integrity. The set() method overwrites the existing element at the specified index with the new element. The get() method returns the elment at the specified index. The remove() method RETURNS AND REMOVES the elment at the specified index. The clear() method simply deletes all the elements in the list.

With an array, when you try to insert an element at the specified index, it overwrites the existing element.

The constructors are as follows:
ArrayList<ReferenceType>();
LinkedList<ReferenceType>();

Methods:
add(int index, Object elment)
add(Object Element) //appends an object to the end of the list
get(int index)
set(int index, Object element)
remove(int index)
clear()

From what I can see, you are trying to write your own ArrayList. Trust me, something is already written.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1