Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Still continuing advice

Posted on 2004-10-03
26
Medium Priority
?
213 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 1000 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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

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 …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

715 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