Im working on an assignment where I have a bag of objects- the data type isnt specified but this bag can hold integers, strings, or doubles, etc. Depending on what type goes in the bag, i have to make sure when i print the contents of the bag that everythings in order. My dilemma is i have no idead how to compare objects within an array especially if they are not specified yet? However i was told the way to go about this is when something is inserted in the array, i have to compare it with it's neighbors and move it around accordingly. So if its a bag of strings, the strings have to be printed in ascending order, bag of ints,lowest to highest, ect. I'm at a loss as to how to do this and compare an array of objects without a data type specified yet. My teacher hinted that the code should be in the insert method but I've been at this for days and am coming up dry.
void insert(T newOb){
//code that puts the object in the bag*
//attempt at trying to compare the newBlock to previous block
T previousB = newOb;
T hold2 = null;
int counter = 1; //once insert happens a second time,thats when you start comparing
if(counter ==2){
if(numberOfBlocks>1){
for(int i = 0; i<numberOfBlocks; i ){
if(newOb.compareTo(previousB)<0){
hold2 = pbs[endIndex-i];
pbs[endIndex-i] = pbs[endIndex- (i 1)];
pbs[endIndex-(i 1)] = hold2;
//2 6 2 3
}
}
}
previousB = newOb;
}
}
when i run the code i get an error:
cannot find symbol
**cannot find symbol
if(newBlock.compareTo(previousB)<0){
^
symbol: method compareTo(T)
location: variable newBlock of type T
where T is a type-variable:
T extends Object declared in class PriorityBS
1 error**
CodePudding user response:
