How to resolve IndexOutOfBoundsExc~ in LinkedList...
Here's the executable code:/* Cannot resolve the ClassCastException */
import java.util.LinkedList;
import javax.swing.JOptionPane;
publicclass ProblemList
{
MusicDatabase mDBase;
LinkedList musicList;
public ProblemList()
{
musicList =new LinkedList();
int numOfElems = Integer.parseInt( JOptionPane.showInputDialog(null,
"Enter the number of elements you require\n"+
"this list to have") );
String title, identity, company;int rank = 0;float price = 0.00f;
// Populating the list with user's entries
for(int index = 1; index<=numOfElems; index++)
{
title = JOptionPane.showInputDialog(null,""+index+"Enter song title");
identity = JOptionPane.showInputDialog(null,
""+index+" Enter name of singer/band");
company = JOptionPane.showInputDialog(null,
""+index+" Enter signed/unsigned company");
rank = Integer.parseInt( JOptionPane.showInputDialog(null,
""+index+" Enter rank (a number)") );
price = Float.parseFloat( JOptionPane.showInputDialog(null,
""+index+" Finally, enter price ?) );
mDBase =new MusicDatabase(title, identity, company, rank, price);
musicList.add(mDBase);
}
// Sorting the list by rank
boolean sorted =false;
LinkedList listToSort = musicList;
while(!sorted)
{
sorted =true;
for (int index = 0; index < musicList.size()-1; index++)
{
int temp;
int a = ((MusicDatabase) musicList.get(index)).getRank();
int b = ((MusicDatabase) musicList.get(index+1)).getRank();
System.out.println("A: "+a+"\nB: "+b);
if(compare(a, b) > 0)
/*if ( compare(((MusicDatabase) musicList.get(index)).getRank(),
((MusicDatabase) musicList.get(index+1)).getRank()) > 0)*/
{
temp = ((MusicDatabase) musicList.get(index+1)).getRank();
listToSort.set(
((MusicDatabase) musicList.get(index+1)).getRank(),
((MusicDatabase) musicList.get(index)).getRank());
//((MusicDatabase) (listToSort.get(index))).getRank();
listToSort.set( ((MusicDatabase) listToSort.get(index)).getRank(),
temp);
sorted =false;
}// if
}// for loop
}// while loop
for(int i=0; i<musicList.size(); i++)
System.out.println(""+((MusicDatabase)listToSort.get(i)).getRank());
}// constructor
// Compare ranks
privateint compare(Object a, Object b)
{
String one =""+a, two =""+b;
int aO = Integer.parseInt(one), bO = Integer.parseInt(two);
// compareTo disallows dereferencing of integers, thus
if(aO > bO)
return 1;
elseif(aO == bO)
return 0;
else
return -1;
}
publicstaticvoid main(String args[])
{
ProblemList problems =new ProblemList();
}
}
class MusicDatabase
{
private String songTitle, identity, musicCompany;
privateint rank;
privatefloat priceF;
// Defining the constructor
public MusicDatabase(String title, String name, String company,
int rankingInt,float price)
{
songTitle = title;
identity = name;
musicCompany = company;
rank = rankingInt;
priceF = price;
}// constructor
// Assessors
public String getTitle()
{
return songTitle;
}
public String getName()
{
return identity;
}
public String getCompany()
{
return musicCompany;
}
publicint getRank()
{
return rank;
}
publicfloat getPrice()
{
return priceF;
}
}

