Solved

Even more Advice requested!

Posted on 2004-10-04
42
173 Views
Last Modified: 2010-03-31
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
Comment
Question by:D4Ly
  • 20
  • 18
  • 4
42 Comments
 
LVL 3

Expert Comment

by:CI-Ia0s
ID: 12223278
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
 
LVL 92

Expert Comment

by:objects
ID: 12223282
you need to get the previous element from your arraylist by the looks of it.
0
 
LVL 9

Author Comment

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

Author Comment

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

Author Comment

by:D4Ly
ID: 12223301
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
 
LVL 3

Expert Comment

by:CI-Ia0s
ID: 12223311
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
 
LVL 92

Expert Comment

by:objects
ID: 12223318
you can use the get() method of ArrayList to get the element at the previous index.
0
 
LVL 9

Author Comment

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

Author Comment

by:D4Ly
ID: 12223326
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
 
LVL 3

Expert Comment

by:CI-Ia0s
ID: 12223345
Hrmm. You say myPostion instances... I only see one instance here...
0
 
LVL 3

Expert Comment

by:CI-Ia0s
ID: 12223358
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
 
LVL 9

Author Comment

by:D4Ly
ID: 12223364
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
 
LVL 3

Expert Comment

by:CI-Ia0s
ID: 12223396
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
 
LVL 3

Expert Comment

by:CI-Ia0s
ID: 12223402
I have no idea what you have being input for o and n, so you'll have to deal with that part. ;)
0
 
LVL 92

Expert Comment

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

Author Comment

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

Author Comment

by:D4Ly
ID: 12223420
I can feel the difficulty increasing, up with the points!
0
 
LVL 3

Expert Comment

by:CI-Ia0s
ID: 12223435
So... you need to move each value down one?
0
 
LVL 92

Expert Comment

by:objects
ID: 12223437
sounds like you should just be storing the comparables in the list and not the Position instancers.
0
 
LVL 9

Author Comment

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

Author Comment

by:D4Ly
ID: 12223445
does what i'm asking make sense?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 3

Expert Comment

by:CI-Ia0s
ID: 12223446
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
 
LVL 9

Author Comment

by:D4Ly
ID: 12223469
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
 
LVL 3

Expert Comment

by:CI-Ia0s
ID: 12223489
for (int c = 0; c < arrayList.size(); c++)
if (arrayList.get(c).index() == 1){return arrayList.get(c);}
}

That do it?
0
 
LVL 3

Expert Comment

by:CI-Ia0s
ID: 12223492
Oops! Forgot my first bracket:

for (int c = 0; c < arrayList.size(); c++){
if (arrayList.get(c).index() == 1){return arrayList.get(c);}
}
0
 
LVL 3

Expert Comment

by:CI-Ia0s
ID: 12223493
Oh, and 1 would be prev, right?
0
 
LVL 9

Author Comment

by:D4Ly
ID: 12223531
won't that return type int? i need to return the position with that int as its index value,
0
 
LVL 3

Expert Comment

by:CI-Ia0s
ID: 12223533
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
 
LVL 9

Author Comment

by:D4Ly
ID: 12223538
no, thats the thing, there are no objects in my arraylist...just integers, refrencing positions
0
 
LVL 3

Expert Comment

by:CI-Ia0s
ID: 12223544
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
 
LVL 3

Expert Comment

by:CI-Ia0s
ID: 12223546
You mean Integers? You can't store ints in an ArrayList...

Where are all the myPositions/Positions stored then?
0
 
LVL 9

Author Comment

by:D4Ly
ID: 12223553
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
 
LVL 3

Expert Comment

by:CI-Ia0s
ID: 12223558
But are the Positions/myPositions themselves in an array? If not I don't think this is gonna work...
0
 
LVL 9

Author Comment

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

Author Comment

by:D4Ly
ID: 12223571
ok, i'm not saying i can't do that, i'm just saying i don't know how to approach this.
0
 
LVL 9

Author Comment

by:D4Ly
ID: 12223575
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
 
LVL 3

Expert Comment

by:CI-Ia0s
ID: 12223641
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
 
LVL 3

Expert Comment

by:CI-Ia0s
ID: 12223644
Well, it's sort of like a bubble sort... :P
0
 
LVL 3

Accepted Solution

by:
CI-Ia0s earned 400 total points
ID: 12223648
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
 
LVL 3

Expert Comment

by:CI-Ia0s
ID: 12223653
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
 
LVL 9

Author Comment

by:D4Ly
ID: 12225853
looks great, thanks very much!!!
0
 
LVL 3

Expert Comment

by:CI-Ia0s
ID: 12233227
Glad to help. :)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
triangle challenge 4 77
java set up 1 46
Core Java. What output will be and why ? 6 58
GUI builder for Eclipse? 8 10
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now