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

时间:2023-01-27 19:00:13

It's possible add an int and an String in the array ? I said in the same array.

可以在数组中添加一个int和一个String吗?我在同一个阵列中说。

7 个解决方案

#1


6  

No, Java is a strongly-typed language. So you cannot add a String and an int to the same array if the array is typed as either String or int.

不,Java是一种强类型语言。因此,如果将数组类型化为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. 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,则可以将String和Integer(整数文字将自动装箱)添加到同一个数组中。这不建议使用,这可能表明您应该更多地考虑您的设计。您需要问自己的第一个问题是您需要这样做的原因。如果确实有正当的理由,那么最好从一个转换为另一个而不是将数组类型化为Object。

Having a catch-call array where you can shove in any object in a bad idea for many reasons:

有一个catch-call数组,你可以在一个坏主意中推入任何对象有很多原因:

  • 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. How would you know what it is? You need to inspect its type explicitly. This is an extremely cumbersome and unmaintainable design.
  • 由于对象之间没有分离,因此从数组中提取的任何内容都是Object。你怎么知道它是什么?您需要明确检查其类型。这是一种非常麻烦且难以维护的设计。
  • 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. 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.
  • 混淆绝不是一个正当理由。代码应清晰,易于阅读,易于维护且易于理解(适用于您和其他任何阅读代码的人)。任何看起来模糊或“聪明”的代码都需要被重写或广泛记录,以解释其“聪明”的原因。至于混淆源代码,这是一个非问题,因为无论如何你将分发.class文件。您可以通过反编译器运行它来查看源代码。您无法在源代码级别对代码进行令人满意的混淆;你只会让你或其他任何人难以维持。混淆可以在字节码级别完成,因此不适用于这种情况。

#2


3  

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

它必须是对象数组

#3


1  

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´. But due to autoboxing and unboxing you wont notice the difference. The tradeoff is of course performance and garbage collector overhead.

请注意,42不是int而是'Integer'。但由于自动装箱和拆箱,你不会注意到差异。权衡当然是性能和垃圾收集器开销。

Also the array must be of type Object[], not of type String[] nor of type int[].

此外,数组必须是Object []类型,不是String []类型,也不是int []类型。

#4


0  

In your string array you could have "123" and then convert it to an int later when you need it.

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

#5


0  

You can't add a primitive types (including int) to an array with Objects such as String. However, autoboxing of int to Integer will make this possible if you declare an Object[] array.

您不能使用String等对象将基本类型(包括int)添加到数组中。但是,如果声明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作为类的属性将起作用。

#6


0  

You can use java.util.ArrayList to do this. You will need to make sure that you check carefully what you are getting when you pull items out though.

您可以使用java.util.ArrayList执行此操作。你需要确保仔细检查当你拉出物品时会得到什么。

#7


0  

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.

请注意,这不一定是习惯的好习惯,但它确实有应用程序。

#1


6  

No, Java is a strongly-typed language. So you cannot add a String and an int to the same array if the array is typed as either String or int.

不,Java是一种强类型语言。因此,如果将数组类型化为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. 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,则可以将String和Integer(整数文字将自动装箱)添加到同一个数组中。这不建议使用,这可能表明您应该更多地考虑您的设计。您需要问自己的第一个问题是您需要这样做的原因。如果确实有正当的理由,那么最好从一个转换为另一个而不是将数组类型化为Object。

Having a catch-call array where you can shove in any object in a bad idea for many reasons:

有一个catch-call数组,你可以在一个坏主意中推入任何对象有很多原因:

  • 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. How would you know what it is? You need to inspect its type explicitly. This is an extremely cumbersome and unmaintainable design.
  • 由于对象之间没有分离,因此从数组中提取的任何内容都是Object。你怎么知道它是什么?您需要明确检查其类型。这是一种非常麻烦且难以维护的设计。
  • 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. 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.
  • 混淆绝不是一个正当理由。代码应清晰,易于阅读,易于维护且易于理解(适用于您和其他任何阅读代码的人)。任何看起来模糊或“聪明”的代码都需要被重写或广泛记录,以解释其“聪明”的原因。至于混淆源代码,这是一个非问题,因为无论如何你将分发.class文件。您可以通过反编译器运行它来查看源代码。您无法在源代码级别对代码进行令人满意的混淆;你只会让你或其他任何人难以维持。混淆可以在字节码级别完成,因此不适用于这种情况。

#2


3  

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

它必须是对象数组

#3


1  

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´. But due to autoboxing and unboxing you wont notice the difference. The tradeoff is of course performance and garbage collector overhead.

请注意,42不是int而是'Integer'。但由于自动装箱和拆箱,你不会注意到差异。权衡当然是性能和垃圾收集器开销。

Also the array must be of type Object[], not of type String[] nor of type int[].

此外,数组必须是Object []类型,不是String []类型,也不是int []类型。

#4


0  

In your string array you could have "123" and then convert it to an int later when you need it.

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

#5


0  

You can't add a primitive types (including int) to an array with Objects such as String. However, autoboxing of int to Integer will make this possible if you declare an Object[] array.

您不能使用String等对象将基本类型(包括int)添加到数组中。但是,如果声明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作为类的属性将起作用。

#6


0  

You can use java.util.ArrayList to do this. You will need to make sure that you check carefully what you are getting when you pull items out though.

您可以使用java.util.ArrayList执行此操作。你需要确保仔细检查当你拉出物品时会得到什么。

#7


0  

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.

请注意,这不一定是习惯的好习惯,但它确实有应用程序。