• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Printing the index of an array

 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I figured I would start a new thread on this because I am sure I am not the only one who could benefit from this.

I wrote a program that allows for some array modifications, and then allows the user to print that array to the screen.

My current design uses a count to list the array elements, however the count gets out of whack as the arrays are manipulated.

I am thinking that a better solution would be to print the index of the array as the counter/ id of the array element.

I found this bit of code, however it uses the value of the array to print the index/ rather than the index

I am wondering if there is a way to print the index of the array based on the index of the array, that way you could print the index to the screen while you go through the for loop.
 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sounds like a better solution would be to forget arrays and pick the most appropriate Collection for the problem you are trying to solve (a simple ArrayList would probably do it).

"index" could be a negative number (you do realise that, yes?).

I am wondering if there is a way to print the index of the array based on the index of the array


I do not follow that at all, but I will admit to having serious brain-fade today. The index is what you use to get the value from an array, that index can simply be dumped to standard out (or wherever you want).
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No no, this one lost me too ;)

Maybe stating the problem you're trying to solve would help--it sounds like you're trying to implement what's basically a collection in an array and track indexes yourself. If that's the case it might be easiest to wrap it up inside its own class so your mainline code doesn't have to worry about it--but as Jason says if you're doing that you might as well just use a collection.
 
Mike Osterhout
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ya i guess that was a bit confusing.
I am just trying to print the index of the array as a number, while getting that number based on the actual index rather than the value of the index.

how would you go about doing this? " that index can simply be dumped to standard out (or wherever you want)."

because doing this system.out.println(newinv[i]); //would print the value and not the index

 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jason Irwin wrote:Sounds like a better solution would be to forget arrays and pick the most appropriate Collection for the problem you are trying to solve (a simple ArrayList would probably do it).


If you're allowed to use this, I agree. It would simplify things greatly.

I am wondering if there is a way to print the index of the array based on the index of the array


I do not follow that at all, but I will admit to having serious brain-fade today...


This has nothing to do with brain-fade. That statement by the original poster is incomprehensible.
 
Mike Osterhout
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
haha, sorry to confuse everyone.

I am simply trying to print the index of the array element to the screen. The code I originally posted is able to do this by using the value of the element.

I want to be able to do this by using the index of the array instead, that way you could use the for loop to print the index along with the values of the element. FYI my array is an array of objects.

edit* I cant use an arraylist or collection. I hope I am making some sense now
 
Jason Irwin
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Osterhout wrote:ya i guess that was a bit confusing.
I am just trying to print the index of the array as a number, while getting that number based on the actual index rather than the value of the index.


The index of an array is always a number - it's can't be anything else! And I take it you actually mean "value at that index".

because doing this system.out.println(newinv[i]); //would print the value and not the index


Try...
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What you mean is a linearSearchInArray method. You can put it in the MikesUtilities classNote this method will depend on your overriding the equals method correctly.
 
Mike Osterhout
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jason- I think your solution might be alright. Sometimes I tend to over think things... The bottom line is, I needed my count/id to match the index of the array and int count was not working so I figured there might be a way to... and I will say my confusing line again b/c I am not sure how else to put it, "access/print the index of the array based on the index of the array".

Campbell- I will investigate that further, right now that seems rather confusing for me. I will mess around with it to see how it works tho.
 
Jason Irwin
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

"access/print the index of the array based on the index of the array".


I think you may not be using the word "index" correctly.

The only way to access an array is by index. That's it. Not by value, not size, not memory offset or anything wild. Just index. That's your lot*.
When you access an array by its index what you get back (assuming it's not out of range or anything) is the value at that index. That's it. Nothing else. Not even the index is returned to you, you just get the value.
Do you understand now why you request make no sense?

Do you mean you want to iterate (loop - like with a for, while or do-while) over the array, printing out the index and the value in the array at that index?

*There are some methods on certain classes that can do fancy things in memory with native code...but I don't think we need to get into that just now!

// There must be some way to make the index equal to i if the target is found, but I can't think of it just at the moment.


Yeah Campbell....sure.
 
Mike Osterhout
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jason Irwin wrote:

"access/print the index of the array based on the index of the array".


I think you may not be using the word "index" correctly.

The only way to access an array is by index. That's it. Not by value, not size, not memory offset or anything wild. Just index. That's your lot*.
When you access an array by its index what you get back (assuming it's not out of range or anything) is the value at that index. That's it. Nothing else. Not even the index is returned to you, you just get the value.
Do you understand now why you request make no sense?

Do you mean you want to iterate (loop - like with a for, while or do-while) over the array, printing out the index and the value in the array at that index?

*There are some methods on certain classes that can do fancy things in memory with native code...but I don't think we need to get into that just now!



Yes I wanted to iterate over the array to print the indexes.

I understand that the only way to access the array is by index, however I was stumped as to how I could access/print that index. The original code in my original post could access/print that number(the index) by using the value of the index/what the index is holding. I was wondering how you could do that same thing by using the index instead of a value/what the index is holding...

I really dont see how this is that confusing, however I want to make sure everyone is on the same page, and I am not sure that has been accomplished.
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jason Irwin wrote:

// There must be some way to make the index equal to i if the target is found, but I can't think of it just at the moment.


Yeah Campbell....sure.

I've remembered how to do it now
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Yes I wanted to iterate over the array to print the indexes.


You just want to print the indexes? The indexes of *what*?
 
Mike Osterhout
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the indexes of the array.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Osterhout wrote:the indexes of the array.



Arrays are indexed via a number -- so an array of size 5 has the indexes 0, 1, 2, 3, and 4. Is that what you want?

Henry
 
Jason Irwin
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Osterhout wrote:the indexes of the array.


The indexes of any array are integer values from 0 to (array length - 1). It's that simple. They are the number used to get a value from an array "String value = myArray[index]". That's what we all understand by the word "index".

You clearly do not want the index, so what is it you do want? Use an example, write a paragraph; but misusing the word "index" is just confusing everyone and no one can help you!
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jason Irwin wrote:

. . . I can't think of it just at the moment.


Yeah Campbell....sure.

So he really wants something different?
 
Jason Irwin
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I looked at the other thread and I think I get the misunderstanding. I'll try my best....

myArray:
index: 0 | 1 | 2 | 3
value: A | B | C | D


I want to iterate over this and print out all the values, along with their index, to standard output. Dead easy Output:
0 - A
1 - B
2 - C
3 - D


Some other code then makes modifies n myArray, removing all the values that equal "C". I run the new myArray through the code above and the output is:
0 - A
1 - B
2 - D


"D" is now at index 2. It was at index 3 - and I think that might be where the confusion lies. I think Mike wants to always display "3" as the index for "D", so his index really isn't an index at all; it's a unique id. And that is just never going to work without some other "look-up" somewhere.

So he really needs 2 arrays
dataArray:
index: 0 | 1 | 2 | 3
value: A | B | C | D


idArray: (this matches the index in the dataArray to an id [value] that should be displayed).
index: 0 | 1 | 2 | 3
value: 0 | 1 | 2 | 3


And then, when C is removed, they should become
dataArray:
index: 0 | 1 | 2
value: A | B | D


idArray:
index: 0 | 1 | 2
value: 0 | 1 | 3


And thus the output code would beIs that about right Mike?

I know giving full answers is bad form, but it's about the only way I can think of getting across what an index actually is.
 
Mike Osterhout
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the final code that I ended up with. With these two posts I was not looking for a unique ID for the elements in the array, I was looking for a way to remove an element and then reprint the array with all null values at the back, and non null at the front. I ran into an issue while using int count to number the elements, so I figured there would be an easy way to reference/print the index of the array. It turns out that just using the i from the for loop essentially does the same thing.

 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jason Irwin wrote: . . .
I know giving full answers is bad form, but it's about the only way I can think of getting across what an index actually is.

If the discussion has gone on this long it sometimes becomes necessary to give a straight answer.
 
Jason Irwin
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mike - your code doesn't match your description. There will be no "null values at the back", just a new array that has one less value.

Also, this chunk of code makes no sense. You will be replacing "newinv" after the loop, why not just do it will accomplish the same thing and is more efficient.

Also, "deletedinv" is a bad name. To me that says "items that have been deleted", but in your code they are actually items that have been preserved.

I am still not clear what you mean by "reference/print the index of the array". The index is simply the number you use to access the array (i, j or a in your code).
 
reply
    Bookmark Topic Watch Topic
  • New Topic