GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes array index in  java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "array index in  java" Watch "array index in  java" New topic
Author

array index in java

naga raaju
Greenhorn

Joined: Mar 14, 2008
Posts: 29
i have an array like this

double d[]={23,34,55,66,7,7,78,43,35,72,11};

i need to find out array index like 0,1,2,3 etc using java code.

can any one give idea

bye
Naga
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1282

Hi Naga,

unfortunately I don't really understand what you are going to do. Could you please explain a little more?

If you want the array element at index 0, 1, 2, ... that's simple:

Marco
naga raaju
Greenhorn

Joined: Mar 14, 2008
Posts: 29
thanks for reply

that is not my question.

how to find out indexes of array.

means 0,1,2,3

thanks and regards
naga
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1282

Do you mean you want to know how much elements an array has? For this an array has special attribute: array.length - There are no parentheses like in String.length().

I hope now this is what you wanted. Otherwise just let me know, we're gonna find your answer

Marco
naga raaju
Greenhorn

Joined: Mar 14, 2008
Posts: 29
hi,
thanks for your help.
how to find out array index.

a[5]={33,44,55,66,64}\\

33 stores 0 index
44 strores 1 index. like this.

print index postions of my array.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39070
    
  23
Go through the Java Tutorial and look specially at arrays and the bit about repetition and the for loop.

You will have to set up an (int) index = -1, then go through your array looking for 33 and if you find 33 put the place you find it in index. Otherwise index remains at -1.
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1282

OK, now I got it

You want the corresponding index of an element in your array. Unfortunately there's no way an array directly supports this. You would have to search through the array each time you want to know an index. But that's probably not the best idea.

I'd recommend you to use one of the List classes provided by the collections framework. The List interface has a method indexOf(Object o) which directly gives you the index of an object in your list. In contrast to an array you have to use wrapper classes like Integer when using collections but with auto-boxing this shouldn't be a problem

Marco
naga raaju
Greenhorn

Joined: Mar 14, 2008
Posts: 29
can you give small example .


thanks
Naga
Anubhav Anand
Ranch Hand

Joined: May 18, 2007
Posts: 341

*I* think it is not feasible to get the indexof an element in an elementary array type.

Well, Vectors can be one solution.



The better placed solution will be to use the one of the Collection classes. But, as a beginner stick to vectors.

Hope that helps.

[Added the example]
[ April 03, 2008: Message edited by: Anubhav Anand ]
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1282

It's not that different to other collections classes. Just use the following and the above example of Anubhav works as well:

Anyway Vector was adapted to implement the List interface as of Java v1.2 so there's no big difference despite the internal advantages and disadvantages of each List implementation regarding synchronization or performance.

Marco
Anubhav Anand
Ranch Hand

Joined: May 18, 2007
Posts: 341

I agree with Marco.
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1763
    
    7

If switching to a class in the Collections Framework isn't an option, you could always use the appropriate overloaded binarySearch() method of the java.util.Arrays class to retrieve the index of a specific element quite efficiently.


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
naga raaju
Greenhorn

Joined: Mar 14, 2008
Posts: 29
hi

a[]={4,5,6,7,8,9};

i wanna print

for(int j=0;j<a.length;j++)
{
System.out.println("::"+a[j]);
}

output :4,5,6,7,8,9

my requirement is

how to print 4,5,6,7,8,9 index using for loop.

thanks
Naga
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1282

In this case the corresponding index should be obvious You use j as an index parameter for a[], so j is obviously your current index for each iteration.
naga raaju
Greenhorn

Joined: Mar 14, 2008
Posts: 29
can you give example

by
naga
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3603
    
  15


Joanne
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
Rephrased post by naga raaju:
I have an array like this

double d[]={23,34,55,66,7,7,78,43,35,72,11};

I want to determine the index in the array of a given value.

bye
Naga


The value of 7 is in there twice: that will be problematic.

Java 1.5
autoboxed double -> Double requires the .0



[ April 03, 2008: Message edited by: Bill Shirley ]
[ April 03, 2008: Message edited by: Bill Shirley ]

Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
 
GeeCON Prague 2014
 
subject: array index in java