我试图了解Scala中的类型参数.我们来看下面的一般例子:
def func1[T](a : T) : T = a
Run Code Online (Sandbox Code Playgroud)
我知道func1接受任何类型的1个参数,并返回相同类型的参数.我不明白的原因是:
def func1[T]
Run Code Online (Sandbox Code Playgroud)
为什么[T]在function1之后?在func1之后我们可以简单地在没有[T]的情况下编写它,例如:
def func1(a : T) : T = a
Run Code Online (Sandbox Code Playgroud)
1)在func1之后,[T]意味着什么?为什么我们把它放在那里?
2)为什么我们对课程做同样的事情?
class MyClass[T]{...}
Run Code Online (Sandbox Code Playgroud)
我的意思是MyClass实例化是MyClass类型.那里[T]意味着什么?你不是说我有一个MyClass类型的布尔类,你说我有一个类型为MyClass的对象吗?
提前致谢.
我看到一些将Java与整数相乘的奇怪行为。我正在做一些编码练习,然后进行了以下嘶嘶声类型的练习。要求:给定一个整数,编写一个函数,该函数查找3的每个乘积的乘积,该乘积小于给定的整数(5的任何乘积除外)。例如,给定17,我们要返回12 * 9 * 6 * 3(= 1944年)。我写了以下内容:
public int findProduct(int limit) {
int product = 1;
for(int n = 3; n < limit; n = n + 3) {
if (n % 5 != 0) {
product = product * n;
}
}
return product;
}
Run Code Online (Sandbox Code Playgroud)
这适用于少量数字。但是在测试中,我发现一旦达到33以上,返回值就会消失。例如,如果我调用限制为36的函数,则它将返回-1.466221696E9。这就是我感到困惑的地方。我乘以正整数,结果某种程度上是负的。
但是,我发现,如果声明一个double值,它似乎总是会返回正确的结果。
public double findProduct(int limit) {
double product = 1;
for(int n = 3; n < limit; n = n + 3) {
if (n % 5 != …Run Code Online (Sandbox Code Playgroud) 美好的一天,伙计们。我正在 flutter 中制作一个足球比分实时数据应用程序。我不知道如何解决这个问题,如果我使用 http.get,我将不得不每次刷新才能获取最新数据。我不知道 StreamBuilder 是否有效以及如何进行。在此先感谢您的帮助。
我将静态数据存储在枚举中。它们可以有很多,但它们具有相同的结构,由特殊的接口指定,以简化所有它们的工作。我在 Swift 中使用了类似的逻辑并且它有效,但 Kotlin 不允许我使用这样的逻辑。
interface DataElement {
val code: UInt
val name: String
}
enum class DataEnum1: DataElement {
Apple {
override val code: UInt
get() = 1u
},
Orange {
override val code: UInt
get() = 2u
},
Watermelon {
override val code: UInt
get() = 3u
}
}
enum class DataEnum2: DataElement {
Blueberry {
override val code: UInt
get() = 4u
},
Strawberry {
override val code: UInt
get() = 5u
},
Blackberry {
override val …Run Code Online (Sandbox Code Playgroud) 我想知道为什么有两种不同的输出:
double a = 88.0;
System.out.println(a + 10); // 98.0
double result = 88.0;
System.out.println("The result is " + result + 10); // The result is 88.010
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Apache Spark中的自定义累加器来累加一个集合。结果应具有Set [String]类型。为此,我创建了自定义累加器:
object SetAccumulatorParam extends AccumulatorParam[Set[String]] {
def addInPlace(r1: mutable.Set[String], r2: mutable.Set[String]): mutable.Set[String] = {
r1 ++= r2
}
def zero(initialValue: mutable.Set[String]): mutable.Set[String] = {
Set()
}
}
Run Code Online (Sandbox Code Playgroud)
但是我无法实例化这种类型的变量。
val tags = sc.accumulator(Set(""))(SetAccumulatorParam)
Run Code Online (Sandbox Code Playgroud)
导致错误。请帮助。
required: org.apache.spark.AccumulatorParam[Set[String]]
Run Code Online (Sandbox Code Playgroud) 我有一个类似的文字"$ $abc $$abc ${a} ${}".我想完全禁用字符串的字符串插值,而不是$从字符串中转义每个字符串.我该怎么办?在Scala中,您声明一个字符串,其中启用插值,s"$ $abc $$abc ${a} ${}"而不插入普通字符串.
当我编译我的代码时,它显示 E 无法转换为 int。
public static <E> void sort(E[] ar){
int swap;
for (int i = 0; i < ar.length; i++) {
for (int j = i; j < ar.length - 1; j++) {
if (ar[j].compareTo(ar[j + 1])) {
swap = ar[j];
ar[j] = ar[j + 1];
ar[j + 1] = swap;
}
}
}
}
Run Code Online (Sandbox Code Playgroud) 我正在学习Java,至少我正在努力.现在我做了一个应用程序,你需要猜测用math.random生成的随机数.
这是代码:
import java.util.Scanner;
public class var {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
//var definitie
int player;
int rnummer;
rnummer = (int) (Math.random() * 50 + 1);
System.out.println("Raad het nummer!");
player = keyboard.nextInt();
System.out.println(rnummer);
//goed geraden check
if (player == rnummer) {
System.out.println("Helaas, dat is niet juist.");
player = keyboard.nextInt();
}
System.out.println("Goed geraden!");
}
}
Run Code Online (Sandbox Code Playgroud)
现在有一个问题,每次你猜它是对的,你不能猜错.我无法弄清楚问题..它必须非常简单.
我在java中弄湿了脚,我偶然发现了一个问题.我怀疑可能已经有了它的答案,但我太知道我应该寻找的是什么 - 这也意味着我的术语可能不正确.
在下面的代码中,我试图从一个单词数组中创建一个随机选择,然后使用if语句显示或不显示单词.问题是,虽然满足if语句的条件(获得字符串"cat"或字符串"dog"),但操作显示任何列出的单词,而不是"cat"或"dog"
我怀疑当我System.out.println(exampleWord());在第10行使用时,例程从数组中获取一个新值,实际上忽略了if语句.
最简单的方法是什么?
import java.util.Random;
public class Phrasing {
String word;
public static void main(String[] args) {
int i = 1;
while (i < 9) {
if ("cat".equals(exampleWord()) || "dog".equals(exampleWord())) {
System.out.println(exampleWord());
i++;
}
}
}
public static String exampleWord() {
String[] listedWords = {"cat", "dog", "horse", "fish", "turtle", "mouse"};
Random random = new Random();
int index = random.nextInt(listedWords.length);
Phrasing wordOutput;
wordOutput = new Phrasing();
wordOutput.word = listedWords[index];
return (wordOutput.word);
}
}
Run Code Online (Sandbox Code Playgroud) 我想根据条件将元素列表附加到另一个列表.例如:找到下面的代码.
package test
object main {
def main(args: Array[String]): Unit ={
val a = List(1,2,3,4,5)
val b= List[Int]()
for(x <- a){
if (x>3){
b:+x
}
}
println(b)
}
}
Run Code Online (Sandbox Code Playgroud)
当我执行这个我得到空列表.
我有一个谓词列表 ( List<Predicate<String>>),我想使用or方法将其链接到一个谓词中。我如何使用 Stream API 来实现这一目标?
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
double[] numbers= new double [12];
numbers[0]=0;
numbers[1]=0;
numbers[2]=0;
numbers[3]=0;
numbers[4]=0;
numbers[5]=0;
numbers[6]=0;
numbers[7]=0;
numbers[8]=0;
numbers[9]=0;
numbers[10]=0;
numbers[11]=0;
for (double i=0;i<numbers.length;i++){
numbers[(int)i]= in.nextDouble();
}
double max = numbers[0];
double min = numbers[0];
for (double i=0;i<numbers.length;i++){
if (numbers[(int)i] > max){
max = numbers[(int)i];
}
}
for (double i=0;i<numbers.length;i++){
if (numbers[(int)i] < min){
min = numbers[(int)i];
}
}
double total = 0;
double average = 0;
for (int i=0; i<numbers.length; i++) { …Run Code Online (Sandbox Code Playgroud) java ×7
scala ×3
kotlin ×2
accumulator ×1
android ×1
apache-spark ×1
api ×1
arrays ×1
enums ×1
flutter ×1
flutter-web ×1
if-statement ×1
interface ×1
math ×1
predicate ×1
rdd ×1
stream ×1
types ×1