简体   繁体   English

Java-是否可以在数组中添加int和String?

[英]Java - It's possible add an int and an String in the array?

It's possible add an int and an String in the array ? 可以在数组中添加一个int和一个String吗? I said in the same array. 我在同一阵子说。

No, Java is a strongly-typed language. 不,Java是一种强类型的语言。 So you cannot add a String and an int to the same array if the array is typed as either String or int . 因此,如果将数组键入为Stringint则不能将Stringint添加到同一数组。

However if your array is typed as Object , you can add a String and an Integer (an integer literal will be autoboxed) to that same array. 但是,如果将数组键入为Object ,则可以将StringInteger (将自动装箱整数文字)添加到同一数组。 This is not recommended and is probably a sign that you should think more about your design. 不建议这样做,这可能表明您应该多考虑设计。 The first question you need to ask yourself is why you need to do this. 您需要问自己的第一个问题是为什么需要这样做。 If you do have a valid reason, then it would be better to convert from one to the other instead of having an array typed as Object . 如果确实有合理的理由,那么最好将一个转换为另一个,而不是将数组键入为Object

Having a catch-call array where you can shove in any object in a bad idea for many reasons: 有一个catch调用数组,可以出于很多原因在一个坏主意中推入任何对象:

  • You are enforcing no separation between the objects. 您正在强制对象之间没有分隔。 Are the objects actually related to each other? 这些对象实际上彼此相关吗? If so you type then using an interface or create an abstract class that each of the types extend. 如果是这样,您可以使用接口进行键入,或者创建每个类型都可以扩展的抽象类。
  • Since you have no separation between the objects, anything you pull out of the array is an Object . 由于对象之间没有分隔,因此从数组中拉出的任何对象都是Object How would you know what it is? 您怎么知道它是什么? You need to inspect its type explicitly. 您需要显式检查其类型。 This is an extremely cumbersome and unmaintainable design. 这是一个非常麻烦且无法维护的设计。
  • You essentially end up losing type-safety and will not be able to benefit from type-mismatch errors that will show up during compilation. 本质上,您最终将失去类型安全性,并且将无法从编译期间会出现的类型不匹配错误中受益。 This will hide possible errors in your code where you may have forgotten to inspect the type, or where you are casting an object to the wrong type. 这将在您可能忘记检查类型的代码或将对象强制转换为错误类型的代码中隐藏可能的错误。 This can lead to all kinds of nightmarish bugs. 这可能导致各种噩梦般的错误。
  • Your code is going to be littered with explicit checks and casts and will be unmaintainable (by you or anyone else). 您的代码将进行明确的检查和强制转换,并且将无法维护(您或其他任何人)。
  • Your code leaks abstraction everywhere. 您的代码到处都会泄漏抽象。 No one can look at the array and realize what the array contains. 没有人可以看一下数组并意识到数组包含的内容。 Anyone who uses your code needs to remember an overwhelming amount of detail as to what types of objects the array can contain. 使用您的代码的任何人都需要记住有关数组可以包含哪些类型的对象的大量细节。
  • Obfuscation is never a valid reason. 混淆永远不是有效的理由。 Code should be clear, easy to read, easy to maintain, and easy to understand (for you and for anyone else who will read your code). 代码应该清晰,易于阅读,易于维护并且易于理解(对于您以及将要阅读您代码的任何其他人)。 Any code that looks obfuscated or is "clever" either needs to be rewritten or documented extensively to explain the reason for its "cleverness". 任何看起来模糊或“聪明”的代码都需要重写或广泛记录以解释其“聪明”的原因。 As far as obfuscating the source, it is a non-issue since you're going to be distributing the .class files anyway. 至于混淆源代码,这不是问题,因为无论如何您都将分发.class文件。 You can run that through a decompiler to look at the source code. 您可以通过反编译器运行该代码以查看源代码。 There is nothing you can do at the source level to satisfactorily obfuscate your code; 您无法在源代码级别上令人满意地混淆代码。 you're only going to make it difficult for you or anyone else to maintain. 您只会使您或其他任何人难以维护。 Obfuscation can be done at the byte-code level and so that doesn't really apply to this situation. 混淆可以在字节码级别进行,因此这实际上不适用于这种情况。

Yes it is possible, but it is not good practice. 是的,有可能,但这不是一个好习惯。

Object[] myObjects = new Object[] {array1[i], array2[i], "name1", value1, value2, "name2",  value1, value....};

It must be array of objects 它必须是对象数组

Strictly speaking: No. 严格来说:不。
Otherwise: Yes for most practical purposes: 否则:出于大多数实际目的:

Object[] array = { 42, "foo" };

Please note, that the 42 is not an int but an `Integer´. 请注意, 42不是int而是“整数”。 But due to autoboxing and unboxing you wont notice the difference. 但是由于自动装箱和拆箱,您将不会注意到差异。 The tradeoff is of course performance and garbage collector overhead. 权衡当然是性能和垃圾收集器开销。

Also the array must be of type Object[] , not of type String[] nor of type int[] . 同样,数组必须是Object[]类型,而不是String[]类型,也不是int[]类型。

在字符串数组中,您可以输入“ 123”,然后在需要时将其转换为int。

You can't add a primitive types (including int) to an array with Objects such as String. 您不能将基本类型(包括int)添加到带有String之类的对象的数组中。 However, autoboxing of int to Integer will make this possible if you declare an Object[] array. 但是,如果声明一个Object []数组,则将int自动装箱为Integer将使这成为可能。

Object[] array = new Object[2];
array[0] = "Hello";
array[1] = 42;

Though I wouldn't recommend doing this if modeling this String and int as attributes of a class would work. 虽然我不建议这样做,如果将此String和int建模为类的属性可以工作的话。

Yes it definitely is possible, just have an array of raw objects. 是的,绝对有可能,仅包含一系列原始对象。

For example: 例如:

Object[] arr = new Object[10];
arr[0] = 10; // boxed to Integer class
arr[1] = "foo"; // String class

Then you can use instanceof to determine the type of object stored at a particular index. 然后,您可以使用instanceof来确定存储在特定索引处的对象的类型。

For example: 例如:

if (arr[0] instanceof Integer) ((Integer) arr[0]) += 10;

Note that this is not necessarily a good practise to get used to, but it does have applications. 请注意,这不一定是习惯的好习惯,但是它确实有应用程序。

You can use java.util.ArrayList to do this. 您可以使用java.util.ArrayList来执行此操作。 You will need to make sure that you check carefully what you are getting when you pull items out though. 不过,您需要确保在拉出物品时仔细检查所得到的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM