Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 181
  • Last Modified:

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.
0
D4Ly
Asked:
D4Ly
  • 20
  • 18
  • 4
1 Solution
 
CI-Ia0sCommented:
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?
0
 
objectsCommented:
you need to get the previous element from your arraylist by the looks of it.
0
 
D4LyAuthor Commented:
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.
http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_21153763.html
http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_21153798.html
http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_21154070.html
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
D4LyAuthor Commented:
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?
0
 
D4LyAuthor Commented:
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.
0
 
CI-Ia0sCommented:
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...
0
 
objectsCommented:
you can use the get() method of ArrayList to get the element at the previous index.
0
 
D4LyAuthor Commented:
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.
0
 
D4LyAuthor Commented:
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?
0
 
CI-Ia0sCommented:
Hrmm. You say myPostion instances... I only see one instance here...
0
 
CI-Ia0sCommented:
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.
0
 
D4LyAuthor Commented:
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.
0
 
CI-Ia0sCommented:
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.*/
}
0
 
CI-Ia0sCommented:
I have no idea what you have being input for o and n, so you'll have to deal with that part. ;)
0
 
objectsCommented:
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);
0
 
D4LyAuthor Commented:
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.
0
 
D4LyAuthor Commented:
I can feel the difficulty increasing, up with the points!
0
 
CI-Ia0sCommented:
So... you need to move each value down one?
0
 
objectsCommented:
sounds like you should just be storing the comparables in the list and not the Position instancers.
0
 
D4LyAuthor Commented:
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.
0
 
D4LyAuthor Commented:
does what i'm asking make sense?
0
 
CI-Ia0sCommented:
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?
0
 
D4LyAuthor Commented:
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.
0
 
CI-Ia0sCommented:
for (int c = 0; c < arrayList.size(); c++)
if (arrayList.get(c).index() == 1){return arrayList.get(c);}
}

That do it?
0
 
CI-Ia0sCommented:
Oops! Forgot my first bracket:

for (int c = 0; c < arrayList.size(); c++){
if (arrayList.get(c).index() == 1){return arrayList.get(c);}
}
0
 
CI-Ia0sCommented:
Oh, and 1 would be prev, right?
0
 
D4LyAuthor Commented:
won't that return type int? i need to return the position with that int as its index value,
0
 
CI-Ia0sCommented:
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.
0
 
D4LyAuthor Commented:
no, thats the thing, there are no objects in my arraylist...just integers, refrencing positions
0
 
CI-Ia0sCommented:
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.
0
 
CI-Ia0sCommented:
You mean Integers? You can't store ints in an ArrayList...

Where are all the myPositions/Positions stored then?
0
 
D4LyAuthor Commented:
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.
0
 
CI-Ia0sCommented:
But are the Positions/myPositions themselves in an array? If not I don't think this is gonna work...
0
 
D4LyAuthor Commented:
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.
0
 
D4LyAuthor Commented:
ok, i'm not saying i can't do that, i'm just saying i don't know how to approach this.
0
 
D4LyAuthor Commented:
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?
0
 
CI-Ia0sCommented:
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();
}
0
 
CI-Ia0sCommented:
Well, it's sort of like a bubble sort... :P
0
 
CI-Ia0sCommented:
Typo... take this code instead:

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] = myPositionObjectToInsert;
for (int c = insertPoint + 1; c <= positionArray.length; c++){
temp2 = positionArray[c].clone();
positionArray[c] = temp.clone();
temp = positionArray[c+1].clone();
positionArray[c+1] = temp2.clone();
}
0
 
CI-Ia0sCommented:
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.
0
 
D4LyAuthor Commented:
looks great, thanks very much!!!
0
 
CI-Ia0sCommented:
Glad to help. :)
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 20
  • 18
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now