I'm doing a university assignment where I need to store information about students (ID, names, GPA etc) and some items have to be unique, such as ID per student. My original plan was to use ArrayList and check with .contains() but my professor has prohibited the use of ArrayLists. What data structure should I use to achieve equivalent outcome?
Asked
Active
Viewed 36 times
1
-
1We can only guess, as there's many possibilities, many of which probably also fall under the "ArrayList ban". You could use a `LinkedList`, but that's probably also forbidden. You could use an array and use `System.copyOf` to copy it into a bigger one when you need it resized. You could build your own linked list or other data structure. It really depends on what the current goal is in your course. So if you just learned about arrays, then chances are that's what you're meant to use. Also if the ID is meant to be unique, you could store it in a `Map` (such as a `HashMap`). – Joachim Sauer Jan 28 '21 at 15:23
-
It is also possible to use maps or sets – Jan 28 '21 at 15:24
-
1The point of the exercise is almost certainly to write your own ad-hoc implementation of an extensible array/vector using the basic Java array, rather than using existing abstract data type implementations. – Konrad Rudolph Jan 28 '21 at 15:25
-
@JoachimSauer we never really learned directly about arrays and usage, just told we had to use them which is kind of annoying. I will have a look into LinkedList as they havent been mentioned, thanks for your help :) EDIT: Just asked my professor and we cannot use a LinkedList as you correctly suggested :/ – Ethan Gallagher Jan 28 '21 at 15:26
-
@Bohemian It’s entirely unclear whether that question is related. — In fact, using a `Set` is almost certainly also not allowed in this assignment. – Konrad Rudolph Jan 28 '21 at 15:28
-
@KonradRudolph crystal clear to me. OP should use a Set of ID, or a Map of ID to the object would be more useful. Only ArrayLists are banned - you can’t assume other collection are too. – Bohemian Jan 28 '21 at 15:31
-
@EthanGallagher: then instead of asking *us* what you should use and asking the professor what you *can't use* ask them what they *intend you to use*. My guess is that @KonradRudolph is right and that you're meant to build your own class that works similar to ArrayList, but I can't be sure and it could aos be what Bohemian thinks (i.e. use a `Map` from ID to some data object). – Joachim Sauer Jan 28 '21 at 15:33
-
2@Bohemian I mean unless you’re a magician it’s absolutely *not* crystal clear. This is a programming exercise, and we don’t know enough about the context; but it seems clear that the students haven’t yet had an introduction into ADTs (they haven’t even studied linked lists yet) — see my initial comment. Your confidence is utterly unwarranted. – Konrad Rudolph Jan 28 '21 at 15:33
-
@JoachimSauer I did ask him, he said anything but an ArrayList. As we are given the data beforehand, I could hard code it but I want to leave that as a last resort and try and find another solution. A map is looking like my best option as the IDs and names are linked so is a good solution. – Ethan Gallagher Jan 28 '21 at 15:35
-
@EthanGallagher: under the literal interpretation of "anything but `ArrayList`", I'd say that a `HashMap` is the correct choice, though it makes me wonder about the order of things you learn in this course. (And then again "anything but `ArrayList`" interpreted literally would allow `LinkedList`, which we know to be forbidden, so take that statement with a grain of salt). – Joachim Sauer Jan 28 '21 at 15:36