Solved

Still continuing advice

Posted on 2004-10-03
26
207 Views
Last Modified: 2010-03-31
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
0
Comment
Question by:D4Ly
  • 14
  • 7
  • 5
26 Comments
 
LVL 9

Author Comment

by:D4Ly
ID: 12212161
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
 
LVL 9

Author Comment

by:D4Ly
ID: 12212188
public Comparable remove(Position p) {
         int i;
         Object remVal;
         i=((Integer)((Object)p)).intValue();
            return (Comparable)_seq.remove(i);
   }

is that my solution?
0
 
LVL 9

Author Comment

by:D4Ly
ID: 12212472
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
ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

 
LVL 86

Expert Comment

by:CEHJ
ID: 12212927
>>is that my solution?

Shouldn't that be

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

?

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12212939
Actually, the cast is not necessary
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12212953
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
 
LVL 9

Author Comment

by:D4Ly
ID: 12213035
p.element() is type Comparable, and my remove method has type int as its parameter
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12213042
0
 
LVL 9

Author Comment

by:D4Ly
ID: 12213078
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12213091
Well, you should probably have a version with an int parameter
0
 
LVL 9

Author Comment

by:D4Ly
ID: 12213123
i do, but

_seq.remove(p.element());

sends type Comparable to my remove method expecting an int parameter.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12213135
You need both remove(Object) *and* remove(int)
0
 
LVL 9

Author Comment

by:D4Ly
ID: 12213168
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12213181
It might be useful if you could post a link to your full code to date somewhere public
0
 
LVL 9

Author Comment

by:D4Ly
ID: 12213509
dalyjason.com/hw2.rar
0
 
LVL 92

Expert Comment

by:objects
ID: 12213787
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
 
LVL 9

Author Comment

by:D4Ly
ID: 12214210
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
 
LVL 92

Accepted Solution

by:
objects earned 250 total points
ID: 12214242
> 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
 
LVL 9

Author Comment

by:D4Ly
ID: 12214386
ok thanks...more later!
0
 
LVL 9

Author Comment

by:D4Ly
ID: 12214410
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
 
LVL 9

Author Comment

by:D4Ly
ID: 12214420
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
 
LVL 92

Expert Comment

by:objects
ID: 12214455
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
 
LVL 9

Author Comment

by:D4Ly
ID: 12214564
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
 
LVL 92

Expert Comment

by:objects
ID: 12214578
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
 
LVL 9

Author Comment

by:D4Ly
ID: 12214590
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
 
LVL 92

Expert Comment

by:objects
ID: 12215100
ok, then that should be about right then.
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to implement Singleton Design Pattern in Java.
This video teaches viewers about errors in exception handling.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question