Link to home
Start Free TrialLog in
Avatar of D4Ly
D4Ly

asked on

Even more Advice requested!

public Position getPrev(Position p){
    myPosition n;
    n = (myPosition) p;
    int prev;
    prev = n.index()-1;
    if(prev<0){
       return null;
    }else{
         //HERE
    }
   }

here's my getPrev() method. I've made some more progress, and understand things considerablly more. So, I have an arrayList that stores index references to instances of myPosition. myPosition implements Position, and contains variables for its index and an object to store. My problem occurs //HERE because I don't know how to iterate through the myPosition instances...I don't know how to reference them, and I don't know how to return something of type p.

I'f I'm not making sense, let me know how I can be clearer.
Avatar of CI-Ia0s
CI-Ia0s

I'm not sure I understand the question. What needs to occur "//HERE"? If there is any other information that might be needed (such as from another thread, as you seem to be hinting) could you explain that or link to the other thread?
Avatar of Mick Barry
you need to get the previous element from your arraylist by the looks of it.
Avatar of D4Ly

ASKER

surely...//HERE returns an instance of position, namely the position with an index value 1 less than the Position p passed to the getPrev method...here's all the old posts.
https://www.experts-exchange.com/questions/21153763/Looking-for-advice.html
https://www.experts-exchange.com/questions/21153798/Continuing-Advice-requested.html
https://www.experts-exchange.com/questions/21154070/Still-continuing-advice.html
Avatar of D4Ly

ASKER

right, I cast Position p to be of type myPosition so I can access the index variable inside p. I then take that index, decrement by one,* go to my array and grab the index located in the previous slot in the array, and return the Position with THAT index. but how do i do from * on?
Avatar of D4Ly

ASKER

i know how to get the index for the position in question out of the array, but I don't know what to return....meaning, i can't figure out how to iterate through all the position instances until the index variable in the position = the index from the array.
So... p contains a bunch an array with a bunch of values, and you want to keep going through the array and...? *is still a bit confused. I'll look over those links for now...
you can use the get() method of ArrayList to get the element at the previous index.
Avatar of D4Ly

ASKER

p is an instance of myPosition passed to the getPrev() method. here's myPosition:

public class myPosition implements Position{

       private Comparable c;
       private int index;

         public myPosition(Comparable o, int i)
         {
            c = o;
            index = i;
         }

         public Comparable element ()
         {
             return c;
         }
        
         public int index(){
                     return index;
         }
}


now, my method needs to take the index value out of here, and find the slot in the array (which is elsewhere in a different class) that contains this value. I then go to the previous slot in the array and grab its value.  THEN (the part i don't get) I need to sort through the myPosition instances until I find the one with an index variable equal to the new index value from the array.  and then i need to return that position.
Avatar of D4Ly

ASKER

right, i got that part now...so i have the new index value, but how do i iterate through my positions to search for the one with the equivalent index?
Hrmm. You say myPostion instances... I only see one instance here...
Waita sec... You have multiple myPositions with index values which you'd like to check against prev?

In that case, you'll have to check every single one, one by one (i.e. if (pos1.index() == prev){}, etc.). If there's an array of myPositions you can use a for-loop.
Avatar of D4Ly

ASKER

right! BUT, when i instantiate the instances, i just say new myPosition(o, n) o being an object and n being an int. So, i have no way of knowing what their names are...i think.
Make an array of them. Then they'll just be arrayName[0], arrayName[1], etc.

Then you can do:

myPosition[] arrayName = new myPosition[100]; //100 is however many myPositions there are
for( int c = 0; c < numberOfInstances; c++){
arrayName[c] = new myPosition(o, n);
}

That loop would just create a bunch of myPositions. You'd want one like this to check them against prev:

for( int c = 0; c < numberOfInstances; c++){
if (arrayName[c].index() == prev){/*Do whatever you want once you've found a match. Perhaps store c in a different variable so you know what position the matching value is in.*/
}
I have no idea what you have being input for o and n, so you'll have to deal with that part. ;)
why would the index of the item in the array be different from its actual index in the array?

ie. why can't you just use:

Object previous = list.get(prev);
Avatar of D4Ly

ASKER

for instance, I insert 5 new myPosition instances. so, their indexes are 0,1,2,3,4 and those index values are also found in slots 0,1,2,3,4 in the arrayList as well. now, i call a remove() method to get rid of myPosition instance #2... my arrayList will now read 0,1,3,4 but will be in corresponding slots 0,1,2,3....the length of the array has changed, and 1 now points to instance 3 instead of 2 and vice versa for 4 going 3 getting the previous position.
Avatar of D4Ly

ASKER

I can feel the difficulty increasing, up with the points!
So... you need to move each value down one?
sounds like you should just be storing the comparables in the list and not the Position instancers.
Avatar of D4Ly

ASKER

i'm just storing references to the positions (pointers) in the list, not positions themselves.  this is how i must create my Positional Container using an arraylist.
Avatar of D4Ly

ASKER

does what i'm asking make sense?
I understand that you have an arrayList like this:
Field:  0 1 2 3
Value: 0 1 3 4

Now what do you need to do with this?
Avatar of D4Ly

ASKER

ok, each value indexes a position...meaning in the first myPosition() instance I create, its index variable will equal 0.

Now, say I want to getPrev(Position p) from this array:
Field:  0 1 2 3 4
Value: 0 1 3 4 5

where the instance of myPosition being passed (p) contains index = 3;

I will then take that instance, go to the array, and find the field with a value of 3...in this case its field 2

I decrement by 1 and so I know the previous is in field 1, aka the myPosition containing index 1 (getting 1 from the value stored in field 1 of the array)

THEN, where the //HERE mark is in my getPrev method, I need a way to loop through all instances of myPosition, checking their index variables for the one equal to 1.

Then I need to return that position.
for (int c = 0; c < arrayList.size(); c++)
if (arrayList.get(c).index() == 1){return arrayList.get(c);}
}

That do it?
Oops! Forgot my first bracket:

for (int c = 0; c < arrayList.size(); c++){
if (arrayList.get(c).index() == 1){return arrayList.get(c);}
}
Oh, and 1 would be prev, right?
Avatar of D4Ly

ASKER

won't that return type int? i need to return the position with that int as its index value,
That would return the object at position c in the array (which would have an index value of 1 or prev, whichever you put on the other side of the ==). The objects in your ArrayList are all myPositions, so would return a myPosition.
Avatar of D4Ly

ASKER

no, thats the thing, there are no objects in my arraylist...just integers, refrencing positions
Hrmm... I see the return type is Position... I think you could typecast, though I'm not sure...

return (Position)arrayList.get(c);

P.S. Put a default return statement after the loop so the compiler doesn't complain. This return statement should never be executed.
You mean Integers? You can't store ints in an ArrayList...

Where are all the myPositions/Positions stored then?
Avatar of D4Ly

ASKER

we're still mis-communicating. so, each instance of the myPosition class contains a variable index. when myPosition is instantiated, its index value is set and this value is put into the next available slot in the array as a reference. the array elements are simply pointers, or references to one another so that methods like getPrev() know what the previous  instance of myPosition should be according to its index value.
But are the Positions/myPositions themselves in an array? If not I don't think this is gonna work...
Avatar of D4Ly

ASKER

when i'm talking of arrayList, i am speaking of an arrayList.java class I create on my own, containing all of the methods java's arrayList utility contains...I am not allowed to use this utility, so i have my own insert method, remove method, etc...

THATS my question!! i have no clue! here is what my insert statement is:

public Position insert(Comparable o) {
               return new myPosition(o, _seq.insert(o));
   }

and this is what i've been told about this:

Almost. Remember that on a list, the position is the list node itself. The position has "permancence". It lives as long as the object it refers to. In the code above, the element could outlive the position that "points" to it. Your position above does indeed refer to the element. But it suffers from this annoying problem that if we modify the ArrayList by adding anything in front or behind, the relative name will be changed.

wow, i'm so confused...and my insert method may be very wrong...as I see it does insert an object o into the arrayList.
Avatar of D4Ly

ASKER

ok, i'm not saying i can't do that, i'm just saying i don't know how to approach this.
Avatar of D4Ly

ASKER

haha, my array is type Comparable right now...should I make this type myPosition? or Position? and then input the actual position into the array elements? then the index values of each myPosition would be pointers to array elements?
Ah, so you're creating your own ArrayList? Ok, well all the objects need to be put into Positions (or myPositions) and then these Positions (or myPositions) need to be put into an array.

Then, to insert something, do it like a bubble sort:


myPosition temp = positionArray[insertPoint].clone(); //Redefine clone and make sure that you copy the information stored in all your objects, not just the memory addresses
myPosition temp2;
positionArray[insertPoint+1] = myPositionToInsert;
for (int c = insertPoint; c <= positionArray.length; c++){
temp2 = positionArray[c].clone();
positionArray[c] = temp.clone();
temp = positionArray[c+1].clone();
positionArray[c+1] = temp2.clone();
}
Well, it's sort of like a bubble sort... :P
ASKER CERTIFIED SOLUTION
Avatar of CI-Ia0s
CI-Ia0s

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry, but it's getting late and I have to head to bed. Tell me how this works and if objects hasn't got you all set by morning I'll do my best. G'night.
Avatar of D4Ly

ASKER

looks great, thanks very much!!!
Glad to help. :)