• 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

Usage of Array in enterprise systems

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, I have a question about the usage of array data structure in real-world app design. We know that we use different data structures for the ever-growing type of data (creating accounts on social media sites/bank accounts, inventory management, hospital management, etc. where data is continuously growing).

And we use an array where the data is fixed. So, here in Coderranch, the sheriffs, marshalls, and other mastermind software engineers have been building really robust enterprise-level software or web apps. From your decades of experience, could you explain in which condition you applied array data structure? I mean, for bank management, for hospital management, and inventory management in which section the array can be applied for better performance?
 
Marshal
Posts: 79394
377
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Most of us wouldn't use arrays if it is possible to use a List instead. Arrays pose problems, for example that they are implicitly mutable.
 
Saloon Keeper
Posts: 10779
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I hardly ever use arrays any more. If your data is not a fixed size then there are dynamic structures that are a better fit. I suppose you could use arrays for look up tables but even that, in an enterprise setting, is probably better handled by a small database table. My only recent uses of arrays were games which involved 2D arrays to represent a grid of the playing field. GUI layouts that are organized as rows, columns, or grids can be facilitated nicely by arrays.
 
Saloon Keeper
Posts: 27852
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note that for other languages, different rules may apply.

In Java, an Array is a (small-c) collection of immutable size but mutable content, including allowing null elements.

It is extremely efficient when you need a data structure whose size is fixed for the life of the application (or at least the Array object). Memory occupancy is relatively small and random element access time is minimal.

It becomes less efficient if you need to change the length of the array, and the more often you need to change it, the less efficient it is.

In addition to vanilla Arrays, there is the ArrayList. At the moment, I believe that ArrayList's internal storage is an Array, but as a List, the length is mutable. That still has the same liabilities as resizing a vanilla Array, but the methods are pre-coded and automatic and the re-sizing strategy is built in. ArrayList is a (large-C) Collection, however, so it also can partake in the rich support environment that Java Collections enjoy.

I use Arrays for a lot of things. For example, I may retrieve a list of items from a database to display in a table or menu on a web page. When I do the retrieval, I get a Collection, but I know its size and it won't change for the life of the current web response action, so I may allocate an array of that size and decant the data into the array. Or, I may just use the original returned Collection directly, depending on context.

Really, for me, it's less a matter of Array/Not-Array (Collection) as a matter of which collection-type object suits me purpose for the task at hand.

 
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I worked a few years on a financial exchange written in Java and arrays were used quite extensively in that system.

This was a high frequency trading platform so performance was a key requirement, and one of the main problems we had was Garbage Collection. The GC would do a stop the world clean up for 30 seconds at a time during which time nobody was trading. 30 seconds is a long time in that domain.

One way we tackled that problem was to use primitives and that's where arrays come in. Arrays can hold primitives, where Collections only hold Objects. That is you can have an array of items of type int, but if you used a List your items would need to be of type Integer.

Of course using primitives for monetary values comes with its own problems and required some special handling, but it was a price worth paying, apparently.
 
Campbell Ritchie
Marshal
Posts: 79394
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:. . . ArrayList's internal storage is an Array . . .

Yes, it says so in its documentation:-

Resizable-array implementation of the List interface. . . .

Since there is really no such thing as  a resizeable array, it adds elements until the backing array is full and then copies them into a larger array. I think older implementations used System#arraycopy(...) but newer implementation use Arrays#copyOfRange(...).
 
Tim Holloway
Saloon Keeper
Posts: 27852
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One reason why "I think's". Stuff changes. Even if it's in the Javadocs, if it's internal, it is very unwise to assume that what your current JVM/OS/Hardware does will always be how it does/stores it. Anything that meets the external spec is fair game, and if you want your graphics card to bit-blit copy arrays instead of running a CPU loop, that should not break application code as long as the application code doesn't assume how the magic is done.

Incidentally, blt-blit array copies date back to at least 1985 (Commodore Amiga), and that's not counting the various generations of block transfer instructions handled by special microcode in the IBM System/360 and /370 mainframes dating back to the 1960s. Earlier than that is unknown to me.
 
Campbell Ritchie
Marshal
Posts: 79394
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:. . . if it's internal . . .

Then it is an implementation detail, which can change without notice. Isn't that called encapsulation? If it is in the Javadocs, doesn't that break encapsulation? But if you say, “The current implementation . . . the implementation may change in future versions,” you are sort of re‑establishing encapsulation by permitting changes.
 
Tim Holloway
Saloon Keeper
Posts: 27852
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The JavaDocs are not the Language Specification nor for that matter, an Implementation Specification.  They reflect the version of the class source that they are embedded in (we hope!), and thus anything referencing internal structure and operation is really only valid for that particular release.

As an extreme example, consider how the java.lang.String class was redesigned internally to reflect the larger Unicode code points (such as those for emoticons). For the most part, the externals of the class remained unchanged, but internally now (at the moment!) there are 2 possible ways to store string data internal to java.lang.String.
 
Campbell Ritchie
Marshal
Posts: 79394
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But the documentation didn't change. The external interface of String was the same and its internal implementation was different. That's how things should work
 
Rancher
Posts: 1093
29
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mahadi,

what i've found is if you have something that you must set a size for, it will eventually be surpassed--array sizes, max allocated space, and etc.  there are a lot of ways to avoid arrays, as many have stated, i echo that idea: avoid them.

Les
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's important to understand that not all data grows, even in a growing system. In a database, for example, it is far more common to add rows to a table than it is to add new columns. In that case if you need an collection of columns, then an array is a good choice, but if you need a collection of rows, you are better using an extendable collection such as a List or Set.

In practice, there is a hierarchy of such choices.
1. fixed in the language, can't be changed without major upset to many programs - data types, keywords, etc.
2. fixed in a program, can't be changed without a re-compile - enums, case statements, fields and methods in a class, etc.
3. fixed in a run of a program, can't be changed without re-starting - command-line arguments, environment variables, etc.
4. fixed for the life of a variable, can't be changed without copying - arrays, strings, etc.
5. not fixed, can be changed at any time - collections, some ad-hoc data structures, external databases
reply
    Bookmark Topic Watch Topic
  • New Topic