[英]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
. 因此,如果将数组键入为String
或int
则不能将String
和int
添加到同一数组。
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
,则可以将String
和Integer
(将自动装箱整数文字)添加到同一数组。 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调用数组,可以出于很多原因在一个坏主意中推入任何对象:
Object
. 由于对象之间没有分隔,因此从数组中拉出的任何对象都是Object
。 How would you know what it is? 您怎么知道它是什么? You need to inspect its type explicitly. 您需要显式检查其类型。 This is an extremely cumbersome and unmaintainable design. 这是一个非常麻烦且无法维护的设计。 .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.