我做了一个着名的生日悖论的小实现,试图找到两个随机生日(这里是1到365之间的整数)之间的碰撞第一次.但它总是返回一个值,比如40和70,这根本不符合统计数据.我的算法或随机int生成器都有问题吗?感谢您的反馈意见.
这是代码:
public static void main(String[] args){
int[] birthday = new int[200];
for(int i = 0; i<20;i++){
Collision(birthday);
}
}
public static int Collision(int birthday[]){
Random rand = new Random();
for(int i = 1; i<birthday.length;i++){
birthday[i] = rand.nextInt(365);
}
int count = 0;
for(int i = 0; i<birthday.length; i++){
for(int j= i+1 ; j<birthday.length; j++){
if (birthday[i] == birthday[j]){
count++;
}
}
}
System.out.print(count+" ");
return count;
}
Run Code Online (Sandbox Code Playgroud)
这是ex的输出:
45 50 60 52 53 53 50 49 37 68 52 …
我正在使用这个博客使用Kotlin编程语言创建一个示例Android项目.我是Kotlin编程的新手.我遇到过这条线,
data class Cats(var data: Data? = null)
我相信它是,创建一个名为Cats具有名为变量的类data.这data: Data? = null意味着什么?我的全班模特是:
data class Cats(var data: Data? = null)
data class Data(var images: ArrayList<Image>? = null)
data class Image(var url: String? = "", var id: String? = "", var source_url: String? = "")
Run Code Online (Sandbox Code Playgroud) 假设您有两个类TestA和TestB.假设TestA扩展了TestB:
public class TestB {
private int intProp;
public int getIntProp() {
return intProp;
}
public void setIntProp(int intProp) {
this.intProp = intProp;
}
}
public class TestA extends TestB {
private String strProp;
public String getStrProp() {
return strProp;
}
public void setStrProp(String strProp) {
this.strProp = strProp;
}
}
Run Code Online (Sandbox Code Playgroud)
现在我创建下一行代码:
var getter1: KFunction1<TestA, Int> = TestA::getIntProp
Run Code Online (Sandbox Code Playgroud)
正如你所看到的,我从TestB的TestA类方法访问:TestA :: getIntProp所以结果是KFunction1的实例,通用参数<TestA,Int>
现在我尝试创建下一行代码
var getter2: KFunction1<TestA, Int> = TestB::getIntProp
Run Code Online (Sandbox Code Playgroud)
它也可以工作和编译,而我希望会有编译错误
尝试使用泛型类型类,但遇到以下问题,即:
类型不匹配:推断的类型是
ChildClass但SuperClass<SuperType>预期
open class SuperClass<T> where T: SuperType {
fun modifySomething(input: T): T {
input.someValue.inc()
return input
}
}
open class SuperType {
val someValue: Int = 0
}
class ChildClass : SuperClass<ChildType>()
class ChildType: SuperType() {
fun getModifiedValue(): Int {
return someValue
}
}
class TestEnvironment {
fun testType(superClass: SuperClass<SuperType>) {
// do something with superClass
}
fun itDoesntWork() {
testType(ChildClass()) // compilation error
}
}
Run Code Online (Sandbox Code Playgroud)
所需的结果是函数 testType(superClass: SuperClass<SuperType>) 应在 不使用 …
class A {
var selectedColor: Int
get() = selectedColor
set(selectedColor) {
this.selectedColor = selectedColor
doSomething()
}
}
class B : A {
override var selectedColor: Int
get() = selectedColor
set(selectedColor) {
this.selectedColor = selectedColor
doSomethingElse()
}
}
Run Code Online (Sandbox Code Playgroud)
关键字覆盖显示错误,说'selectedColor'是最终的,无法覆盖.有什么我想念的吗?
大家好,我有一个简单的问题,我有一个充满元素的 Map,我想在我的 Object 中转换它,让我向您展示一些代码:
这是我的对象:
class Bundle(map: Map<String, Any>) {
var version: String?
var app: String?
var countries: ArrayList<Any>?
var currency: ArrayList<Any>?
var force: Boolean?
var name: String?
var service: ArrayList<Any>?
var money: Int?
init {
version= null
app= null
countries= arrayListOf()
currency= arrayListOf<Any>()
force= true
name = ""
service= arrayListOf<Any>()
money= 0
}
}
Run Code Online (Sandbox Code Playgroud)
还有我想转换的地图:
fun getBundle() {
var db = FirebaseFirestore.getInstance()
val docRef = db.collection("aa").document("bb")
docRef.get().addOnCompleteListener { task ->
if (task.isSuccessful) {
val document = task.result
if (document.exists()) …Run Code Online (Sandbox Code Playgroud) 我需要知道将在科特林地图保存我插入过程中已经进入了条目顺序
示例
val data = mutableMapOf<String,String>()
data.put("some_string_1","data_1")
.
.
.
data.put("some_string_n","data_n")
Run Code Online (Sandbox Code Playgroud)
订单会在地图迭代期间保留吗?
我在这段代码中收到错误:
fun num(num:Int):Int {
if (num > 0){
print(num % 10)
return num / 10
}
} //here an error
Run Code Online (Sandbox Code Playgroud)
错误:(15,1)Kotlin:具有块体('{...}')的函数中需要的'return'表达式
它是什么意思,我该如何解决?
我无法理解Kotlin实际上在做什么:
我的单元测试看起来像这样:
@Test
fun testReadCursorRequest() {
val xml = fromFile()
val parser: ReadCursorRequestParser = ReadCursorRequestParser(xml)
assertEquals(0, parser.status)
assertEquals(134, parser.contacts!!.size)
}
Run Code Online (Sandbox Code Playgroud)
我的解析器看起来像这样
abstract class EnvelopeParser(val xml: String) {
abstract fun parseResponse(response: Element)
init {
parseResponse(xmlFromString(xml))
}
// non-related stuff
}
Run Code Online (Sandbox Code Playgroud)
class ReadCursorRequestParser(xml: String) : EnvelopeParser(xml) {
var contacts: List<AddressBookElementParser> = mutableListOf()
override fun parseResponse(response: Element) {
// here some parsing stuff, fills the contacts-list
println("size is: ${contacts.size}")
}
}
Run Code Online (Sandbox Code Playgroud)
println说size is: 134,单元测试说:java.lang.AssertionError: Expected <134>, actual <0>.
为什么?