Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Determine index from array reference?

Reply
Thread Tools

Determine index from array reference?

 
 
Knute Johnson
Guest
Posts: n/a
 
      01-24-2007
Is there a way to determine the index of an array element given a reference?

Object[] obj = new Object[8];

Object q = obj[2];

Is it possible to get 2 from q other than iterating through all values
of obj[] and testing for equality of the reference?

Thanks,

--

Knute Johnson
email s/nospam/knute/
 
Reply With Quote
 
 
 
 
Jason Cavett
Guest
Posts: n/a
 
      01-24-2007


On Jan 24, 1:27 pm, Knute Johnson <(E-Mail Removed)>
wrote:
> Is there a way to determine the index of an array element given a reference?
>
> Object[] obj = new Object[8];
>
> Object q = obj[2];
>
> Is it possible to get 2 from q other than iterating through all values
> of obj[] and testing for equality of the reference?
>
> Thanks,
>
> --
>
> Knute Johnson
> email s/nospam/knute/


Not to my knowledge. This is why Collection classes were made, though.
Many of the Collection classes offer a method that gives you the index
based on the Object. (I believe it's indexOf(Object object) without
looking up Javadocs.)

 
Reply With Quote
 
 
 
 
Knute Johnson
Guest
Posts: n/a
 
      01-24-2007
Jason Cavett wrote:
>
> On Jan 24, 1:27 pm, Knute Johnson <(E-Mail Removed)>
> wrote:
>> Is there a way to determine the index of an array element given a reference?
>>
>> Object[] obj = new Object[8];
>>
>> Object q = obj[2];
>>
>> Is it possible to get 2 from q other than iterating through all values
>> of obj[] and testing for equality of the reference?
>>
>> Thanks,
>>
>> --
>>
>> Knute Johnson
>> email s/nospam/knute/

>
> Not to my knowledge. This is why Collection classes were made, though.
> Many of the Collection classes offer a method that gives you the index
> based on the Object. (I believe it's indexOf(Object object) without
> looking up Javadocs.)
>


I hadn't really considered it but I guess a Vector would do that nicely.

Thanks,

--

Knute Johnson
email s/nospam/knute/
 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      01-24-2007


On Jan 24, 10:27 am, Knute Johnson <(E-Mail Removed)>
wrote:
> Is there a way to determine the index of an array element given a reference?
>
> Object[] obj = new Object[8];
>
> Object q = obj[2];
>
> Is it possible to get 2 from q other than iterating through all values
> of obj[] and testing for equality of the reference?
>
> Thanks,
>
> --
>
> Knute Johnson
> email s/nospam/knute/

Arrays.asList(array).indexOf(o);

Anyway, don't use Vector, use ArrayList

 
Reply With Quote
 
Mike Schilling
Guest
Posts: n/a
 
      01-24-2007

"Knute Johnson" <(E-Mail Removed)> wrote in message
news:1yNth.296994$(E-Mail Removed)...
> Is there a way to determine the index of an array element given a
> reference?
>
> Object[] obj = new Object[8];
>
> Object q = obj[2];
>
> Is it possible to get 2 from q other than iterating through all values of
> obj[] and testing for equality of the reference?


You could call

Array.asList(obj).indexOf(q)

But all that does is hide the code that iterates through the array. And
note that it checks for object equality, not reference equality. (i.e.
o.equals(p), not o == p)


 
Reply With Quote
 
Mark Rafn
Guest
Posts: n/a
 
      01-25-2007
Knute Johnson <(E-Mail Removed)> wrote:
>Is there a way to determine the index of an array element given a reference?


Other than searching the array, no. And you don't buy anything by converting
it to a Vector or other List and calling indexOf(), because THAT just does a
search after a bunch of copying.


>Object[] obj = new Object[8];
>Object q = obj[2];


>Is it possible to get 2 from q other than iterating through all values
>of obj[] and testing for equality of the reference?


In your example q will be null, which will be the value at ALL indices of the
array. This is possible even for a populated array. for example:

String[] strings = new String[] { "a", "b", "c", "a", "a", "a" };
String aString = strings[3];

The object pointed to by the variable aString is also pointed to by positions
0, 3, 4, and 5 of the array.
--
Mark Rafn http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.dagon.net/>
 
Reply With Quote
 
Patricia Shanahan
Guest
Posts: n/a
 
      01-25-2007
Knute Johnson wrote:
> Is there a way to determine the index of an array element given a
> reference?
>
> Object[] obj = new Object[8];
>
> Object q = obj[2];
>
> Is it possible to get 2 from q other than iterating through all values
> of obj[] and testing for equality of the reference?
>
> Thanks,
>


This type of question always seems a little backwards to me. I tend to
think the other way round. Not "How do I do this with an array?" but "I
need to do these accesses. What data structure should I use?".

Why an array? What other operations are being done on it?

Patricia
 
Reply With Quote
 
Knute Johnson
Guest
Posts: n/a
 
      01-25-2007
Patricia Shanahan wrote:
> Knute Johnson wrote:
>> Is there a way to determine the index of an array element given a
>> reference?
>>
>> Object[] obj = new Object[8];
>>
>> Object q = obj[2];
>>
>> Is it possible to get 2 from q other than iterating through all values
>> of obj[] and testing for equality of the reference?
>>
>> Thanks,
>>

>
> This type of question always seems a little backwards to me. I tend to
> think the other way round. Not "How do I do this with an array?" but "I
> need to do these accesses. What data structure should I use?".
>
> Why an array? What other operations are being done on it?
>
> Patricia


Thanks everybody for your replies. And since Patricia asked the big
question maybe I should give up some more information about what exactly
I'm trying to do.

I asked about an array because that's what I have now. An array of
JTextFields that have ActionListeners attached. In the AL I need to
update another array. So what I had done in the past was to extend
JTextField and add an int variable to hold an index value for the
JTextField. I added a getIndex() method and in my ActionListener I use
that method to acquire the index to modify my other array.

So I could have checked the JTextField reference against all the others
in the array and gotten an index that way but that didn't sound a whole
lot better than the way I was getting it now.

The little program below appears to work with a Vector. What do you
think of that approach?

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

public class test1 {
public static void main(String[] args) {
Vector<JTextField> v = new Vector<JTextField>();
JTextField test = null;

for (int i=0; i<10; i++) {
JTextField tf = new JTextField(" ",10);
v.add(tf);
if (i == 3)
test = tf;
}

System.out.println(v.indexOf(test));
System.out.println(v.indexOf(null));
}
}

Anyway, if anybody has a better idea I'm all ears.

Thanks,

--

Knute Johnson
email s/nospam/knute/
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      01-25-2007
Knute Johnson wrote:
>>> Is there a way to determine the index of an array element given a
>>> reference?


Jason Cavett wrote:
>> Not to my knowledge. This is why Collection classes were made, though.
>> Many of the Collection classes offer a method that gives you the index
>> based on the Object. (I believe it's indexOf(Object object) without
>> looking up Javadocs.)


Knute Johnson wrote:
> I hadn't really considered it but I guess a Vector would do that nicely.


As long as you need synchronized methods.

- Lew
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      01-25-2007
Knute Johnson wrote:
>>> Is there a way to determine the index of an array element given a
>>> reference?


Patricia Shanahan wrote:
>> This type of question always seems a little backwards to me. I tend to
>> think the other way round. Not "How do I do this with an array?" but "I
>> need to do these accesses. What data structure should I use?".
>>
>> Why an array? What other operations are being done on it?


Knute Johnson wrote:
> I asked about an array because that's what I have now. An array of
> JTextFields that have ActionListeners attached. In the AL I need to
> update another array. So what I had done in the past was to extend
> JTextField and add an int variable to hold an index value for the
> JTextField. I added a getIndex() method and in my ActionListener I use
> that method to acquire the index to modify my other array.
>
> So I could have checked the JTextField reference against all the others
> in the array and gotten an index that way but that didn't sound a whole
> lot better than the way I was getting it now.


That is exactly what your posted code does.

> The little program below appears to work with a Vector. What do you
> think of that approach?


Why ignore the advice to use ArrayList?

> public class test1 {
> public static void main(String[] args) {
> Vector<JTextField> v = new Vector<JTextField>();
> JTextField test = null;
>
> for (int i=0; i<10; i++) {
> JTextField tf = new JTextField(" ",10);
> v.add(tf);
> if (i == 3)
> test = tf;
> }
>
> System.out.println(v.indexOf(test));
> System.out.println(v.indexOf(null));
> }
> }


Bear in mind that these are first occurrences of these values in the List.

If you plan to use the result of your "indexOf()" to locate another object,
rather than just println() it, you might consider using a
Map <JTextField, OtherType>. That would have the benefit of constant time
lookup (if you use HashMap) instead of O(n). You also avoid bugs caused by
"parallel" Lists going non-Euclidean.

- Lew
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Making an array wrap, where last index + 1 = first index Shawn W_ Ruby 5 09-16-2009 02:45 PM
sorting index-15, index-9, index-110 "the human way"? Tomasz Chmielewski Perl Misc 4 03-04-2008 05:01 PM
How to determine the tbody index value A Question ASP .Net 1 12-20-2007 02:52 PM
nuby: determine method passed and determine the receiver that received the method Peņa, Botp Ruby 1 01-24-2004 07:51 PM
Determine the index of the datarow in the datagrid when using hyperlinkcolumn debiken ASP .Net Datagrid Control 0 11-17-2003 10:38 PM



Advertisments