Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中以简短的方式在数组中查找整数_Java_Arrays_List_Hashmap_Instantiation - Fatal编程技术网

在Java中以简短的方式在数组中查找整数

在Java中以简短的方式在数组中查找整数,java,arrays,list,hashmap,instantiation,Java,Arrays,List,Hashmap,Instantiation,我已经试过这个帖子了: 我试过这个 int[] temp = {3,9,15,21,27,33,39}; HashSet<Integer> otherBy3 = new HashSet<Integer>(Arrays.asList(temp)); int[]temp={3,9,15,21,27,33,39}; HashSet otherBy3=新的HashSet(Arrays.asList(temp)); 根据我在上述链接上看到的指示我这样做的帖子: HashSet

我已经试过这个帖子了:

我试过这个

int[] temp = {3,9,15,21,27,33,39};
HashSet<Integer> otherBy3 = new HashSet<Integer>(Arrays.asList(temp));
int[]temp={3,9,15,21,27,33,39};
HashSet otherBy3=新的HashSet(Arrays.asList(temp));
根据我在上述链接上看到的指示我这样做的帖子:

HashSet<Integer> set= new HashSet<Integer>(Arrays.asList(intArray));
set.contains(intValue)
HashSet=newhashset(Arrays.asList(intArray));
set.contains(intValue)
但我总是犯这个错误

cannot find symbol
symbol  : constructor HashSet(java.util.List<int[]>)
location: class java.util.HashSet<java.lang.Integer> HashSet<Integer> otherBy3 = new HashSet<Integer>(Arrays.asList(temp));
找不到符号
符号:构造函数哈希集(java.util.List)
位置:class java.util.HashSet HashSet otherBy3=新的HashSet(Arrays.asList(temp));
如有任何帮助,将不胜感激

int[] temp = ...

改变


int更改为Integer,因为您将列表声明为generic asInteger

Integer[] temp = new Integer[] { 3, 9, 15, 21, 27, 33, 39 };
HashSet<Integer> otherBy3 = new HashSet<Integer>(Arrays.asList(temp));
otherBy3.contains(13);
Integer[]temp=新的整数[]{3,9,15,21,27,33,39};
HashSet otherBy3=新的HashSet(Arrays.asList(temp));
其他3.包含(13);

int更改为整数,因为您将列表的泛型声明为整数

Integer[] temp = new Integer[] { 3, 9, 15, 21, 27, 33, 39 };
HashSet<Integer> otherBy3 = new HashSet<Integer>(Arrays.asList(temp));
otherBy3.contains(13);
Integer[]temp=新的整数[]{3,9,15,21,27,33,39};
HashSet otherBy3=新的HashSet(Arrays.asList(temp));
其他3.包含(13);
Integer[]temp={3,9,15,21,27,33,39};
HashSet=newhashset(Arrays.asList(temp));
System.out.println(set.contains(3));
Integer[]temp={3,9,15,21,27,33,39};
HashSet=newhashset(Arrays.asList(temp));
System.out.println(set.contains(3));

要想知道你要问的真正问题是“如何在int[]数组中找到整数”,为什么不直接使用。这是一个单行程序,非常快的O(对数n),是常用的方法。唯一的预请求是您的列表已排序,而您的列表似乎已排序(不是吗?)


要想知道你要问的真正问题是“如何在int[]数组中找到整数”,为什么不直接使用。这是一个单行程序,非常快的O(对数n),是常用的方法。唯一的预请求是您的列表已排序,而您的列表似乎已排序(不是吗?)


我发现构造几个临时对象被认为比简单的异常循环“短”,这有点令人不安:

 int[] temp = {3,9,15,21,27,33,39};
 boolean found = false;
 for (int i=0; i<temp.length && !found; ++i) {
     found = valueToFind == temp[i];         
 }
int[]temp={3,9,15,21,27,33,39};
布尔值=false;

对于(int i=0;i我发现构造几个临时对象被认为比一个简单的异常循环“短”,这有点令人不安:

 int[] temp = {3,9,15,21,27,33,39};
 boolean found = false;
 for (int i=0; i<temp.length && !found; ++i) {
     found = valueToFind == temp[i];         
 }
int[]temp={3,9,15,21,27,33,39};
布尔值=false;

对于(int i=0;该链接上的第一个答案指出当
int[]
数组时会发生什么。asList
(以及为什么它不做你想要的)…该链接上的第一个答案指出
int[]
数组时会发生什么。asList
(以及为什么它不做你想做的事情)…那么a呢?最多需要n个日志。你甚至可以在
数组#binarySearch
中得到一个免费的impl。这需要排序。排序是O(n个日志n)(最多)与简单循环的普通O(n)相比。问题主要是关于“短”(我假设代码行的数量),我只是把这个答案放在这里指出“正在做”大约是作为其他东西的副产品来做的。如果目标是性能,那么你需要更多的上下文来选择最好的方法。好的。列表在问题中排序,但这可能只是偶然的。那么a呢?最多需要logn。你甚至可以在
Arrays\binarySearch中得到一个免费的impl
。这需要排序。对于简单循环,排序最多是O(N log N)而不是普通的O(N)。问题主要是关于“短”(我假设代码行的数量),我给出这个答案是为了指出“只是这样做”它的长度与作为其他东西的副产品来做这件事的长度差不多。如果目标是性能,那么您需要更多的上下文来选择最佳方法。好的。列表在问题中进行了排序,但这可能只是偶然的。
 int[] temp = {3,9,15,21,27,33,39};
 boolean found = false;
 for (int i=0; i<temp.length && !found; ++i) {
     found = valueToFind == temp[i];         
 }