Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

array index in java

 
naga raaju
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49756
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you give small example .


thanks
Naga
 
Anubhav Anand
Ranch Hand
Posts: 341
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
*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
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 341
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Marco.
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
naga raaju
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you give example

by
naga
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Bill Shirley
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic