Still continuing advice

Reference:
http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_21153763.html
http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_21153798.html


Can you help with this one?

public Comparable remove(Position p) {
           _seq.remove(p);
           return new myPosition(p);
   }

this should be my last issue with this problem. Here's what I know. Position p is the slot in the arrayList containing the data I want to remove. My remove method is expecting an int, not a Position.

Do I typecast p to an int value? Is there a different way to approach this? I know I can't call _arr[p] to access my _arr array's value at p when p is of type Position. I also know that typcasting from type Position to type Integer to type Int does not work, or at least i can't figure out how to.  So, after I've figured out how to remove the value from the array in position p, I want to return the value of _arr[p], and i know my remove() method returns type Object. But how do I return type Object as type Position? Thanks again
LVL 9
D4LyAsked:
Who is Participating?
 
objectsConnect With a Mentor Commented:
> so, p.element is going to be a piece of data in the array.

yes

> the remove method is passed p, a method indexOf() will find the slot in the array that that p.element data is found,

yes, indexOf() is a method in ArrayList that returns the index of a specified object

> and return it?

no, it calls the remove() method of ArrayList which removes the element (at the specified index) and returns the object removed at that index.
0
 
D4LyAuthor Commented:
ooo...I've figured out the type Position is of type Object. So, returning myPosition(p) will return and object...but the rest I am still unsure about.
0
 
D4LyAuthor Commented:
public Comparable remove(Position p) {
         int i;
         Object remVal;
         i=((Integer)((Object)p)).intValue();
            return (Comparable)_seq.remove(i);
   }

is that my solution?
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
D4LyAuthor Commented:
why do i get a classCastException error for this? am i approaching this problem incorrectly? getItemAt returns type Comparable (an object).

/**
    * Removes the item at the given Position and returns the object
    * stored there.
    */
public Position getLast(){
            return (Position)_seq.getItemAt(_seq.getLength()-1);
   }
0
 
CEHJCommented:
>>is that my solution?

Shouldn't that be

public Comparable remove(Position p) {
    return (Comparable)_seq.remove(p.element());
}

?

0
 
CEHJCommented:
Actually, the cast is not necessary
0
 
CEHJCommented:
Correction

public Comparable remove(Position p) {
   _seq.remove(p.element());
   return p;
}


and the other one

>>why do i get a classCastException error for this?

Make sure the stored type *is* Position if you want to return that

0
 
D4LyAuthor Commented:
p.element() is type Comparable, and my remove method has type int as its parameter
0
 
CEHJCommented:
0
 
D4LyAuthor Commented:
ah, sorry, should have mentioned...i am using my OWN arrayList.  I have to create all the methods on my own, insert, remove, getlength, etc... so my remove method expects int. i can change what it expects though, but it has to remove _arr[i] from the array.
0
 
CEHJCommented:
Well, you should probably have a version with an int parameter
0
 
D4LyAuthor Commented:
i do, but

_seq.remove(p.element());

sends type Comparable to my remove method expecting an int parameter.
0
 
CEHJCommented:
You need both remove(Object) *and* remove(int)
0
 
D4LyAuthor Commented:
so then in my remove(Object i) method, how can i remove _arr[i], when its not an int, do i have to typecast then?
0
 
CEHJCommented:
It might be useful if you could post a link to your full code to date somewhere public
0
 
D4LyAuthor Commented:
dalyjason.com/hw2.rar
0
 
objectsCommented:
you need to locate the index of the specified Positioin in your list, something like:

public Comparable remove(Position p) {
           return (Comparable) _seq.remove(_seq.indexOf(p.element()));
   }
0
 
D4LyAuthor Commented:
so, p.element is going to be a piece of data in the array. the remove method is passed p, a method indexOf() will find the slot in the array that that p.element data is found, and return it? man i'm lost.
0
 
D4LyAuthor Commented:
ok thanks...more later!
0
 
D4LyAuthor Commented:
This is what i have for my getNext method. However, I don't feel that I am using Position properly...i think i'm approaching it incorrectly...advice?

public Position getNext(Position p){
            int i;
            i=((Integer)((Object)p)).intValue();
            i++;
            if(i>_seq.getLength()-1){
                  return null;
            }else{
                return new myPosition((Comparable)(new Integer(i)));
}
0
 
D4LyAuthor Commented:
public Position getNext(Position p){
        return (Comparable) _seq.getNext(_seq.indexOf(p.element()));
   }

where getNext() is similar to remove(), but doesn't remove the value, and instead grabs the value of the item next to p.element() in the arrayList.

Am I starting to get it? Does this seem like an okay way to approach the problem? am i looking for the VALUE or INDEX in the array of the value when passing p.element()?
0
 
objectsCommented:
getting closer, what you need to do is find the next element in the array and return a Position instance that points to it.
0
 
D4LyAuthor Commented:
public Position getNext(Position p){
        return new myPosition(_seq.getNext(_seq.indexOf(p.element())));
   }  

where _seq.getNext() is a method in my ArrayList class that finds the index of p.element() in the arrayList, then grabs index+1 and returns its value? or its index?
0
 
objectsCommented:
ArrayList doesn't have a getNext() method.
It has a get() method that needs the index of the element to return (see javadoc for more details)
0
 
D4LyAuthor Commented:
I have an arrayList class that i have created (i must do it this way)...in it i can have any method i want...so i would create a getNext() method
0
 
objectsCommented:
ok, then that should be about right then.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.