Solved

Still continuing advice

Posted on 2004-10-03
26
208 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Protect jar file - windows app 2 65
Way to decrease size of apk file 9 87
Configure a Bean in an XML file 4 42
java example issue 5 25
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

828 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