创建没有变量的新对象数组 [英] Creating array of new Object with no variable

查看:47
本文介绍了创建没有变量的新对象数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名主要学习 Java 的学生.

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.

我是否只需要习惯 flufftown 甚至 kitten1 已经消失但 sackOfKittens.get(1) 是等效的想法?

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

把它写出来更有意义.

我猜一个人只是在 Kitten 类中编写方法来完成所有混乱的工作,仅此而已.

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

我错过了什么吗?

推荐答案

您需要放弃必须为您创建的每个对象分配一个符号的想法.符号(变量名)是在编译时定义的,但您可以编写在运行时创建任意数量对象的程序,而无需事先知道如何调用它们.

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.

考虑public static void main(String[] args).您将如何处理为 args 中的每个 String 提供变量名称?你可以试着聪明一点,提前布置一堆变量:

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.

事实上,这就是 main 需要一个 String[] 而不是多个 String 的确切原因.无法预先知道命令行参数的数量,因此需要一个数组.您必须通过它们在数组中的索引来引用它们.

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.

在没有符号的情况下,您可以使用多种方法来查找数据结构中的元素(例如数组、ListMap).

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).

Lists 和数组等数据结构可以迭代.假设你想找到名为"puss"kitten,你可以遍历List中的每一个kitten,直到你找到你想要的:

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!
    }
}

或者,您可以利用强大的 Map 数据结构,根据某些独特的键"属性更快、更轻松地获取对象:

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!

这篇关于创建没有变量的新对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆