0

I'm a student studying mostly Java.

The more I learn about object oriented programming the more I find myself creating arrays of new objects:

ArrayList<kitten> sackOfKittens = new ArrayList<kitten>();
String[] kittenNames = new String[10]; 
//fill kittenNames with cute strings like "flufftown" and "Indie Rocker"

for(int i = 0; i < 10; i++) {
    sackOfKittens.get(i) = new Kitten(kittenNames[i]);
}

This feels funny to me because now I have all these kittens that I'm clearly going to do something with and there's no direct reference to them, or no variable that I named anyway.

Do I just need to get used to the idea that flufftown or even kitten1 is gone but sackOfKittens.get(1) is equivalent?

Writing this out is sort of making more sense of it.

I guess a person just writes methods in the Kitten class to do all the messy and that's that.

Am I missing something?

4
  • ...so, what's the question? Commented Mar 27, 2014 at 19:32
  • 1
    Did you try to compile that code? It won't compile, you'll get an error. Commented Mar 27, 2014 at 19:33
  • 3
    What do you mean by "gone"? In programming, it's normal to deal with large quantities of data, and you can't give each piece of data an individual name. That sounds to me like what you're asking, but I can't tell for sure. Commented Mar 27, 2014 at 19:36
  • if you want to set at some index use set() method of ArrayList or just use add() Commented Mar 27, 2014 at 19:38

2 Answers 2

1

You need to let go of the idea that you must assign a symbol to every object you create. Symbols (variable names) are defined at compilation time, but you can write programs that create an arbitrary number of objects at runtime, without you knowing what to call them in advance.

Consider public static void main(String[] args). How would you handle giving a variable name to every String in args? You could try to be smart and lay out a bunch of variables in advance:

public static void main(String[] args) {
    String s1 = (args.length > 0) ? args[0] : null;
    String s2 = (args.length > 1) ? args[1] : null;
    String s3 = (args.length > 2) ? args[2] : null;
    String s4 = (args.length > 3) ? args[3] : null;
    String s5 = (args.length > 4) ? args[4] : null;
}

But what will you do if you run into more than that? What if you need to support thousands of possible command line arguments? You cannot possibly predict this in advance, of course.

In fact, this is the exact reason why main takes a String[] and not multiple Strings. The number of command line arguments cannot be known in advance, so an array is necessary. And the only way you have to refer to them is by their indices in the array.


In the absence of symbols, you can use many methods of lookup on elements in a data structure (such as an array, a List, or a Map).

Data structures such as Lists and arrays can be iterated over. Say you want to find the kitten named "puss", you can go through each kitten in the List until your find the one you want:

kitten pussTheKitten;
for (kitten k: sackOfKittens) {
    if (k.getName().equals("puss")) {
        pussTheKitten = k; // found it!
    }
}

Alternatively, you can utilize the powerful Map data structure to make it faster and easier to get objects based on some unique "key" attribute:

Map<String, kitten> mapOfKittens = new HashMap<String, kitten>();
for (kitten k: sackOfKittens) {
    mapOfKittens.put(k.getName(), k);
}
kitten pussTheKitten = mapOfKittens.get("puss"); // found it!
Sign up to request clarification or add additional context in comments.

1 Comment

That's a great answer. Thank you for taking the time to explain everything the way you did. It's the kind of answer I was hoping for when I posted, showing a perspective that I don't have yet.
0

I recommend that you learn more about Map which maps keys to their values. In this case you can use the names as the key. This way you don't have to remember that flufftown is at offset 1.

 Map<String, Kitten> map = new HashMap<>();

 map.put("flufftown", new Kitten(...));

then you can get it out by the same key

Kitten myKitten = map.get("flufftown");

1 Comment

Nice. That makes sense. Lot's to think about when choosing a data structure.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.