• Post Reply Bookmark Topic Watch Topic
  • New Topic

Indexing in arrays  RSS feed

 
Sergei Zhylinski
Ranch Hand
Posts: 89
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I'm reading a book called "Data Structures and Algorithms in Java" by R. Lafore. And I wonder whether I can treat an index of an array in Java as a separate field of a record or not. The author divides a single database, which has a special format, into records (in object-oriented programming they correspond to objects), and records, in turn, divides into several fields. One of the fields is special; it is used in records searching and is known as a key.

As far as I understand from the second chapter, an array simply represents a list of some records. They may be ordered or unordered. And indexing is optional. And in chapter 9 in the introduction to hash tables the author describes a sample of the personnel department's database. This database, organized as an array, possesses records about employees; indexes introduce personnel number and therefore are treated as keys of this array.

Thanks in advance.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I find that question hard to understand. An array index in Java® is simply a number between 0 and 2147483646 (I think) inclusive. Yes, you can store a number in a database record of any kind. But why? How are you going to link that number back to a Java® program? How will you know the array hasn't changed and the index is still correct.
I looked through the pdf in the link you helpfully quoted, and could not find anything about storing indices in a database. Are you confusing array indices with indexing of databases, which is something different done in order to speed searching?
 
Paweł Baczyński
Bartender
Posts: 2077
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I find that question hard to understand. An array index in Java® is simply a number between 0 and 2147483646 (I think) inclusive.

http://stackoverflow.com/questions/3038392/do-java-arrays-have-a-maximum-size
There is an answer to the question of maximum array size
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel Pawlowicz wrote: . . .
There is an answer to the question of maximum array size
But it is wrong It depends on the available memory. Neither the JLS nor the JVM specification gives a maximum size.

Anyway, shall we get back to the original subject of the thread?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sergey Zhylinsky wrote:As far as I understand from the second chapter, an array simply represents a list of some records.

Well, that's wrong for a start. It represents a list of values. Now, in Java, each of those values can be an object, so it MAY represent a list of records, but it could just as easily represent a list of numbers, aircraft or chess pieces.

You also need to be very careful when using words like "list" because in Java, a List (java.util.List) means something very specific, and it is NOT the same as an array.

They may be ordered or unordered. And indexing is optional.

Again wrong. If you want a specific element in an array, indexing is most definitely NOT optional. What the book may be referring to is the fact that, since version 1.5, you can now traverse an array - ie, get ALL its elements in sequence - without using indexes.

And in chapter 9 in the introduction to hash tables the author describes a sample of the personnel department's database.

We appear to be getting off-topic. Your original question was about arrays, and a hashed table is most definitely NOT an array.

This database, organized as an array, possesses records about employees; indexes introduce personnel number and therefore are treated as keys of this array.

And now we seem to be straying into the definition of a Map (java.util.Map).

An array is a collection in which each element has a fixed position (determined by - and only by - an index); and the collection as a whole has a fixed size.

I suggest you back up and ask your question again. What exactly is it you want to know?

Winston
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that author uses “ordered” to mean “sorted”.
 
Sergei Zhylinski
Ranch Hand
Posts: 89
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I think that author uses “ordered” to mean “sorted”.


And this is confusing. Page 68 of the book: "Imagine an array in which the data items are arranged in order of ascending key values — that is, with the smallest value at index 0, and each cell holding a value larger than the cell below. Such an array is called an ordered array."

But I suggest that a definition provided relates to a sorted array.

Campbell Ritchie wrote:An array index in Java® is simply a number between 0 and 2147483646 (I think) inclusive. Yes, you can store a number in a database record of any kind. But why? How are you going to link that number back to a Java® program? How will you know the array hasn't changed and the index is still correct.


So index cannot be part of a record in the database, which is organized through an array of Java objects. It is simply used to numerate elements in the array?

Winston Gutkowski wrote:An array is a collection in which each element has a fixed position (determined by - and only by - an index);


May I say, that an array is an ordered collection? And indexing is what makes them "ordered"?

Winston Gutkowski wrote:You also need to be very careful when using words like "list" because in Java, a List (java.util.List) means something very specific, and it is NOT the same as an array.


I know that the List interface is implemented by two Java classes: LinkedList (doubly-linked list implementation, it must be as on the picture at p. 222 on my book) and ArrayList (resizable-array implementation of the List interface.). From above can we say that a simple fixed-sized array is also some kind of a list?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sergey Zhylinsky wrote:
Winston Gutkowski wrote:You also need to be very careful when using words like "list" because in Java, a List (java.util.List) means something very specific, and it is NOT the same as an array.


I know that the List interface is implemented by two Java classes: LinkedList (doubly-linked list implementation, it must be as on the picture at p. 222 on my book) and ArrayList (resizable-array implementation of the List interface.). From above can we say that a simple fixed-sized array is also some kind of a list?



I can't see how you can draw this conclusion -- as it is also possible to implement a sorted tree, a hash table, a graph, along with a ton of other data structures using a few fixed length arrays ... but that doesn't make the array some kind of tree, hash table, graph, etc.

Henry
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sergey Zhylinsky wrote:May I say, that an array is an ordered collection? And indexing is what makes them "ordered"?

I think so; but, to be honest, I still get confused by words like "order" (even after 35 years ) because it means different things in different contexts. Comparable (java.lang.Comparable), for example, says that it provides an "order", but that specifically relates to sorting order.

For an array, all you can really say is that element positions - read indexes - are fixed, and that a for-each loop will always return elements in the same order; and on that basis, your statement is correct.

I know that the List interface is implemented by two Java classes: LinkedList (doubly-linked list implementation, it must be as on the picture at p. 222 on my book) and ArrayList (resizable-array implementation of the List interface.). From above can we say that a simple fixed-sized array is also some kind of a list?

Not really. The difference comes to light when you look at the remove(int) method.
First off: You can't really remove an element from an array, you can only "nullify" it.
Second: The above method changes the indexes of ALL elements to the right of the one that was removed. Again, this is not true of an array.

HIH

Winston
 
Sergei Zhylinski
Ranch Hand
Posts: 89
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to everybody. I emphasized on some things. I've also reread the chapter, devoted to hash tables and now begin to understand the difference between a key and an index.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome It is good to see we can get a few things right occasionally
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Sergey Zhylinsky wrote:May I say, that an array is an ordered collection? And indexing is what makes them "ordered"?

I think so; but, to be honest, I still get confused by words like "order" (even after 35 years ) because it means different things in different contexts. Comparable (java.lang.Comparable), for example, says that it provides an "order", but that specifically relates to sorting order.

Ordered means that the elements are stored in a specific, defined order. There are collections which store elements in a specific order, such as arrays and lists. There are also collections which do not store elements in any particular order, such as sets. An unordered collection is like a bag: it contains a bunch of things, but not in any particular order. If you stick your hand in the bag to take the things out one by one, you don't know in which order you get the elements. And if you put everything back and do it again, the order might be different.

Sorted means that the elements are ordered to some specific rule (for example, alphabetically).

A sorted collection is obviously also an ordered collection; it makes no sense to talk about sorting with regard to an unordered collection. The other way around doesn't always hold; an ordered collection isn't necessarily sorted.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!