如何转换java int[][]为 Integer[][]?.使用java流将一维原始数组转换为对象类型的一维数组似乎很容易。
例如
Integer[] result = IntStream.of( intarray ).boxed().toArray( Integer[]::new );
Run Code Online (Sandbox Code Playgroud)
像上面这样的二维数组有什么办法吗?
我有一个Triple列表,它是用户定义的类。当我使用Comparator它对它进行排序时,显示出奇怪的行为。
考虑一下此片段-
List<Triple> list = new ArrayList<>();
list.add(new Triple(1,12,13)); //adding values to list
list.add(new Triple(11,3,31));
list.add(new Triple(16,6,32));
list.add(new Triple(16,8,32));
list.add(new Triple(16,7,32));
list.add(new Triple(16,9,32));
list.add(new Triple(16,5,32));
list.add(new Triple(7,21,0));
list.add(new Triple(6,22,12));
list.add(new Triple(4,22,13));
list.add(new Triple(2,77,3));
list.add(new Triple(1,8,30));
Run Code Online (Sandbox Code Playgroud)
使用比较器排序
list.sort(
Comparator.comparingInt(Triple::getA)
.thenComparingInt(Triple::getB)
.thenComparing(Triple::getC));
list.forEach(e->System.out.printf("(%d,%d,%d) ",e.getA(),e.getB(),e.getC()));
System.out.println();
//sort A descending if for same A ascending B and for same B ascending C
list.sort(
Comparator.comparingInt(Triple::getA).reversed()
.thenComparingInt(Triple::getB)
.thenComparing(Triple::getC));
list.forEach(e->System.out.printf("(%d,%d,%d) ",e.getA(),e.getB(),e.getC()));
System.out.println();
//sort A ascending if for same A descending B and for same B …Run Code Online (Sandbox Code Playgroud) 我知道java中任何数组的最大大小是Integer.maxValue。因为数组是 int 索引的。但我知道,我们也可以使用移位运算符初始化数组。我试过了,但它抛出了奇怪的输出。
int arr[] = new int[1<<5];
System.out.println(arr.length);
arr = new int[1<<10];
System.out.println(arr.length);
arr = new int[1<<20];
System.out.println(arr.length);
arr = new int[1<<25];
System.out.println(arr.length);
arr = new int[1<<40];
System.out.println(arr.length);
arr = new int[1<<111];
System.out.println(arr.length);
Run Code Online (Sandbox Code Playgroud)
我预计每移动 n 次,数组的结果大小将为 2^n。
32
1024
1048576
33554432
256
32768
Run Code Online (Sandbox Code Playgroud)
因此,如果有任何其他方法可以在适当的大小分配中初始化这样的数组,是否可以通过移动 1100 次来初始化大小为 2^100 的数组?
我有 python Dict,当我使用min(). 看我的代码
scores = {leaf_size: round(get_mae(leaf_size, train_X, val_X, train_y, val_y)) for leaf_size in candidate_max_leaf_nodes}
best_tree_size = min(scores, key=scores.get)
Run Code Online (Sandbox Code Playgroud)
get_mae() 只是一个计算平均绝对误差的函数。
但它显示以下错误
TypeError Traceback (most recent call last)
<ipython-input-48-aa5b1fc6e492> in <module>
8 # print(dict)
9 scores = {leaf_size: round(get_mae(leaf_size, train_X, val_X, train_y, val_y)) for leaf_size in candidate_max_leaf_nodes}
---> 10 best_tree_size = min(scores, key=scores.get)
11 # list = scores.items()
TypeError: 'int' object is not callable
Run Code Online (Sandbox Code Playgroud) 我正在创建一个程序Q#。
问题
你有两个处于状态 |00? 的量子比特。您的任务是为它们创建以下状态:1/3–?(|00?+|01?+|10?) 您必须实现一个操作,该操作将 2 个量子位的数组作为输入并且没有输出。您的解决方案的“输出”是它离开输入量子位的状态。
代码
namespace Solution {
open Microsoft.Quantum.Primitive;
open Microsoft.Quantum.Canon;
open Microsoft.Quantum.Math;
open Microsoft.Quantum.Convert;
operation Solve (qs : Qubit[]) : Unit
{
body
{
Ry(ArcCos(Sqrt(2.0/3.0))*2.0,qs[0]);
(ControlledOnInt(0,H))([qs[0]],qs[1]);
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是当我运行它时会显示以下错误。
错误
CSC : error CS5001: Program does not contain a static 'Main' method suitable for an entry point
[C:\Users\Pawar\Desktop\HK\codeforces\Q#\Solution\Solution.csproj]
Run Code Online (Sandbox Code Playgroud)
所以我试着把EntryPoint()方法声明放在前面。这向我显示了不同的错误
错误 QS6231:无效的入口点。Qubit 类型的值不能用作参数或返回值到入口点。[C:\Users\Pawar\Desktop\HK\codeforces\Q#\Solution\Solution.csproj]
请帮我如何正确运行它?谢谢 ??
考虑以下代码。它用于检查字符串是否具有有效的括号但不使用堆栈。
public boolean isValid(String input) {
while(input.length() != (input = input.replaceAll("\\(\\)|\\[\\]|\\{\\}", "")).length());
return input.isEmpty();
}
Run Code Online (Sandbox Code Playgroud)
但是有点难以理解。这可以简化吗?不添加更多新行?
如果我们想在 javascript 中检查变量的数据类型,我们可以使用typeofoperator 。
var c = 'str' ;
console.log(typeof(c)); // string
c = 123 ;
console.log(typeof(c)); // number
c = {} ;
console.log(typeof(c)) ; // object
Run Code Online (Sandbox Code Playgroud)
我想在Java 8. Java 没有 typeof 运算符,但有instanceof用于检查类型的运算符。
System.out.println("str" instanceof String); // true
Integer a = 12 ;
System.out.println(a instanceof Integer);
Float f = 12.3f
System.out.println(f instanceof Float); // true
Run Code Online (Sandbox Code Playgroud)
我们能做得更好吗?Plusinstanceof不支持原始类型。
有什么办法java 8吗?任何相关的方法将不胜感激。
我正在阅读this ,其中包含旧的java包,例如sun包。我发现其中一些很有用。
其中是sun.security.provider.certpath.Vertex,它代表一个顶点,在 Vertex 类之后是 AdjacencyList ie sun.security.provider.certpath.AdjacencyList,它代表顶点的邻接列表。
从文档说,
AdjacencyList 用于存储认证路径的历史记录。
这是专门为 X500 证书及其路径定义的,并忽略了无法抑制的内部 API 警告?我们可以将这些类用于解决 JDK 1.8 上的问题的通用图吗?
我正在处理BigInteger每个数字的大小为2 ^ 100的一些问题。我需要该数字的第n个数字。我该怎么做?
使用toString()我将BigInteger转换为String,然后获取该数字,但是String的大小最多仅为Int的最大值?
int get(BigInteger b,BigInteger n)
{
return Character.getNumericValue(b.toString().charAt(n.intValue()));
}
Run Code Online (Sandbox Code Playgroud)
因此,此代码仅在BigInteger小于Int max值时才有效。但是在我的情况下,经过某些迭代后,可能是我的BigInteger交叉限制的机会,因此如何在该BigInteger中获得第n个BigInteger数字?
我已经列出了成对的。我想先根据键对它们进行排序,如果键相同,则根据值进行排序。
我尝试了以下代码,但引发了不兼容类型的异常:无法推断类型变量T
List<Pair<Integer,Integer>> list = new ArrayList<>();
list.sort(Comparator.comparingInt(Pair::getKey).thenComparingInt(Pair::getValue);
Run Code Online (Sandbox Code Playgroud)
错误:
不兼容的类型:无法推断类型变量T (参数不匹配;类对中的无效方法引用方法getKey无法应用于所需的给定类型:未找到参数:对象原因:实际和正式参数列表的长度不同)
其中T,K,V是类型变量:T扩展在方法compareInt(ToIntFunction)中声明的对象K扩展在类Pair中声明的对象V扩展在类Pair中声明的对象
java ×8
java-8 ×5
arrays ×2
sorting ×2
.net-core ×1
biginteger ×1
comparator ×1
dictionary ×1
graph ×1
list ×1
matrix ×1
min ×1
object-type ×1
parentheses ×1
primitive ×1
python ×1
q# ×1
stack ×1
string ×1
sun ×1