java 在java中创建一个存储字符串和整数的数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27402861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 11:45:31  来源:igfitidea点击:

Creating an array that stores strings and integers in java

javaarraysobject

提问by Billy

I would like to create an array that stores the names (String) and values (integer) of company stocks, but I don't know how to go about it.

我想创建一个数组来存储公司股票的名称(字符串)和值(整数),但我不知道如何去做。

回答by Stephen C

An Object[]can hold both Stringand Integerobjects. Here's a simple example:

AnObject[]可以同时容纳StringInteger对象。这是一个简单的例子:

    Object[] mixed = new Object[2];
    mixed[0] = "Hi Mum";
    mixed[1] = Integer.valueOf(42);
    ...
    String message = (String) mixed[0];
    Integer answer = (Integer) mixed[1];

However, if you put use an Object[]like this, you will typically need to use instanceofand / or type casts when accessing the elements.

但是,如果Object[]像这样使用 use instanceof,则在访问元素时通常需要使用和/或类型转换。

Any design that routinely involves instanceofand/or type casts needs to be treated with suspicion. In most cases, there is a better (more object-oriented, more efficient, less fragile) way of achieving the same ends.

任何经常涉及instanceof和/或类型转换的设计都需要怀疑。在大多数情况下,有一种更好的(更面向对象、更高效、更不脆弱)的方式来实现相同的目标。

In your particular use-case, it sounds like what you really need is a mapping object that maps from String(names) to Integer(numbers of stocks). And the nice thing about Java is that there are existing library classes that provide this functionality; e.g. the HashMap<K,V>class, with Stringas the key type and Integeras the value type.

在您的特定用例中,听起来您真正需要的是一个从String(名称)映射到Integer(股票数量)的映射对象。 Java 的好处是现有的库类提供了这个功能;例如HashMap<K,V>类,String作为键类型和Integer作为值类型。

Another possibility might be an array, Listor Setof some custom or generic pair class. These have different semantic properties to Maptypes.

另一种可能性可能是一个数组,List或者Set一些自定义或通用对类。它们对Map类型具有不同的语义属性。

回答by Billy

You can either use array declaration or array literal (but only when you declare and affect the variable right away, array literals cannot be used for re-assigning an array).

您可以使用数组声明或数组文字(但仅当您立即声明并影响变量时,数组文字不能用于重新分配数组)。

For primitive types:

对于原始类型:

int[] myIntArray = new int[3];
int[] myIntArray = {1,2,3};
int[] myIntArray = new int[]{1,2,3};

For classes, for example String, it's the same:

对于类,例如 String,它是相同的:

String[] myStringArray = new String[3];
String[] myStringArray = {"a","b","c"};
String[] myStringArray = new String[]{"a","b","c"};

Extra Info:If you are knew to coding in Java here are some tutorials:http://www.youtube.com/watch?v=Cfd9DOnuF9w

额外信息:如果您了解 Java 编码,这里有一些教程:http: //www.youtube.com/watch?v=Cfd9DOnuF9w

-HAPPY CODING!

- 编码快乐!

回答by Rene M.

You have two choices:

你有两个选择:

  1. Use an array.
  1. 使用数组。
public class Value {
  public String name;
  public int number;
}

...
public Value[] values = new Value[10];
....
public class Value {
  public String name;
  public int number;
}

...
public Value[] values = new Value[10];
....
  1. Use a map which has much more comfort, specially you can use the name as key to get a value
  1. 使用更舒适的地图,特别是您可以使用名称作为键来获取值
....
public Map<String, int> valueMap = new HashMap<String,int>();
valueMap.put("Sample",10);
int value = valueMap.get("Sample");
...
....
public Map<String, int> valueMap = new HashMap<String,int>();
valueMap.put("Sample",10);
int value = valueMap.get("Sample");
...

回答by mohsaied

You can use a Map data structure instead of an array. This is basically a type of Collection that has key-value pairs. Your string name can be used as the key and the value is your integer.

您可以使用 Map 数据结构而不是数组。这基本上是一种具有键值对的 Co​​llection 类型。您的字符串名称可以用作键,值是您的整数。

Map<String,Integer> myMap = new HashMap<String, Integer>;

MyMap.put("someone", 6);

Note that using a HashMap has a speed advantage over an array during lookup. The complexity of HashMap lookup is O(log(n)) while that of an array is O(n).

请注意,在查找期间使用 HashMap 比数组具有速度优势。HashMap 查找的复杂度是 O(log(n)) 而数组的复杂度是 O(n)。