r/learnjava • • 6d ago

Building My Custom Data Structure Library Feed-Back pt. 2

A while ago, I posted about a small project I was working on for fun: a Java library for N-dimensional array storage and manipulation.

Originally, I made it for my own game, but eventually decided to put it on GitHub in case anyone else found it useful. I posted here to get feedback, suggestions, and criticism, and I ended up learning quite a bit from the comments.

Since then, I've made several changes, reconsidered some of my original design decisions, and released a new version.

I'd love to get some more feedback: What's good? What's bad? What could be improved? Are there any questions you have about the library or its implementation? I'm open to constructive criticism and suggestions.

I know this kind of library isn't for everyone, and that's perfectly fine. Some people will prefer working directly with Java arrays or existing collections, and I understand that. I'm mainly interested in hearing what you think of the implementation, API, documentation, and overall usefulness.

I'm also interested in feedback on the presentation. I got some comments about the README being hard to read(kinda defeats the purpose, huh?), so I've redesigned it and added more information. Hopefully, that helps.

Anyway, here's the GitHub:

https://github.com/AJ-1-code/JArray

1 Upvotes

22 comments sorted by

View all comments

Show parent comment

1

u/8dot30662386292pow2 4d ago

Generics wouldn't directly accomplish this in Java, since generic types can't be primitive and would still require boxing.

Require boxing yes, but you can still use primitives if you do it right. I have something like this:

public class Container {
    abstract class Storage {
        abstract <T> T get(int index);
    }
    class IntStorage extends Storage {
        int[] data;

        @Override
        Integer get(int index) {
            return data[index];
        }

    }

    class LongStorage extends Storage {
        long[] data;

        @Override
        Long get(int index) {
            return data[index];
        }

    }

    
    Storage storage;

    int getInt(int index) {
        return storage.get(index);
    }
    
    int getLong(int index) {
        return storage.get(index);
    }

}

1

u/fishybusinessdev 4d ago

I'm not against alternative storage architectures. A Storage hierarchy could be a reasonable approach. But I'm not seeing what specifically makes it better for JArray.

Your example still requires separate primitive backing arrays and specialized storage implementations. It demonstrates another way to organize primitive storage, but doesn't establish that it's simpler, faster, or more maintainable than the current implementation.

Also, your generic get() method returns boxed types like Integer, which means it still involves boxing and unboxing when accessing primitive values through that method.

If you see a specific problem with my architecture, I'd genuinely like to hear it. I'm open to changing things when there's a concrete reason to do so.

1

u/8dot30662386292pow2 4d ago

I guess everyone has the same problem: what is the situation where you need arbitrary amount of arbitrary primitives in a single data structure? I may be too deep in the correct way of doing things that I can't understand what is the situation when this is needed.

1

u/fishybusinessdev 4d ago

I think Python's lists are a good example of why heterogeneous containers can be useful. You can store integers, floats, strings, booleans, and even other lists in the same structure.

JArray offers similar flexibility, but extends it to N-dimensional data, with additional support for native primitive storage when the data is homogeneous.

One of the original reasons I started developing JArray was for my own game. I used it for the map system, where having an N-dimensional structure with operations like slicing and manipulating sections of the map was useful.

Of course, strongly typed structures are preferable in plenty of situations. I'm not suggesting heterogeneous storage should replace them. It's simply another useful way to organize and manipulate data, especially when working with dynamically structured datasets.

1

u/8dot30662386292pow2 4d ago

I think Python's lists are a good example of why heterogeneous containers can be useful.

Thats what I'm asking, why is it useful? I see no usefulness: just because it's possible, it should bot be done. Also you can do exactly same thing in java with arraylist, but you absolute should not.

It's nice that it works for your game though. Still not sure what you are doing that you could not do other ways. It kind of feels like you are missing something important that caused you to create this thing, but I'm still not sure what that might be. Writing your own stuff is always great, don't get me wrong. It's just that I've been teaching so long that I've seen all kinds of things being built when the correct thing is not understood.

Of course, strongly typed structures are preferable in plenty of situations. 

Your thing is also strongly typed, having hererogenous objects does not make typing weak.

1

u/fishybusinessdev 3d ago

I think a spreadsheet-like data grid is a reasonable example of where heterogeneous storage can be useful.

Imagine a grid where some cells contain integers, others contain floating-point values, strings, or booleans. You want to preserve those types while still being able to access the data by coordinates, slice regions, and manipulate the grid as an N-dimensional structure.

You can absolutely store those values in an ArrayList<Object> or an Object[][]. I'm not claiming otherwise. The difference is that JArray provides the N-dimensional array operations and views on top of that storage model.

I also agree that heterogeneous storage shouldn't be used just because it's possible. If the data has a known, fixed structure, a class, record, or strongly typed array is often the better choice. I'm not trying to replace those.

As for why I included heterogeneous storage, JArray is intended to be a general-purpose array abstraction, not exclusively a primitive numerical array library. Supporting mixed values is useful for data whose structure isn't necessarily homogeneous, while homogeneous data can still use native primitive storage.

And you're right that heterogeneous storage doesn't automatically mean weak typing. To be precise, JArray preserves the types of its stored values and provides type-specific accessors, although it doesn't enforce a single compile-time element type through generics.

I'm not arguing that every application needs this. I'm explaining the use case the feature is intended to support.