我正在寻找一种最好的方法来迭代 IntArray 索引类似于下面的 JAVA 代码
for (int i = 0; i < arr.length - 1; i++)
Run Code Online (Sandbox Code Playgroud)
我正在使用下面的代码,我不想迭代最后一个元素来避免IndexOutOfBoundException. A.indices允许我迭代所有索引,而我不想像上面那样触摸最后一个元素:
class Solution {
fun isMonotonic(A: IntArray): Boolean {
var increasing : Boolean = false
var decreasing : Boolean = false
for (i in A.indices) {
if (A[i] <= A[i+1]) increasing = true
if (A[i] >= A[i+1]) decreasing = true
}
return increasing && decreasing
}
}
Run Code Online (Sandbox Code Playgroud) 当某些操作发生时,我想禁用芯片组。
目前我的代码是这样的:
R.id.all -> {
chipGroup.clearCheck()
chipGroup.isClickable = false
selectedCategory = null
}
Run Code Online (Sandbox Code Playgroud)
它确实清除了所有检查,但我仍然可以单击并选择筹码。
如何防止所有筹码被点击?
代码目的:Class Pair 可以打印出产品名称和数量,产品名称存储在Class Product
class Pair<T, U>(var product: Product, var quantity: Int) {
for ( (product,quantity) in productAndQuantityList) {
println("Name: ${product.productName}")
println("Quantity: $quantity")
}
}
Run Code Online (Sandbox Code Playgroud)
以上错误:(2, 9) Kotlin:期望成员声明错误:(2, 57) Kotlin:函数声明必须有一个名称
class ShoppingCart{
private val productAndQuantityList = mutableListOf<Pair<Product,Int> >()
...
}
open class Product(
val productName: String,
var basePrice: Double,
open val salesPrice: Double,
val description: String) {
...}
Run Code Online (Sandbox Code Playgroud)
谢谢!
在 Scala 中,您可以使用三个问号延迟实现:
def doSomething(s: String): Int = ???
Run Code Online (Sandbox Code Playgroud)
Kotlin 是否支持这样的功能?
以下代码在 Kotlin 中不起作用:
var s: String = "hello"
s[1]='a'
Run Code Online (Sandbox Code Playgroud)
Kotlin 是否具有可变字符串类型,或者Array<Char>如果我想能够进行编辑,是否必须使用?
我正在开发新闻应用程序,我是 firebase Crashlytics 的新手我想强制崩溃,我想获得崩溃报告,但我没有收到任何崩溃报告,我该如何实现
在我的实施之下
binding.root.setOnClickListener { v ->
FirebaseCrashlytics.getInstance()
val intent = Intent(v.context, DetailActivity::class.java)
intent.putExtra(urlKey, articleList[position].url)
v.context.startActivity(intent)
}
Run Code Online (Sandbox Code Playgroud)
在我的 firebase crashlytics 控制台屏幕截图下方我的 firebasecrashlytics 控制台
我想知道我在哪里犯了错误我怎么能强迫我的应用程序崩溃
有没有办法在 Kotlin 中获取对通用扩展函数/方法的函数引用?
class C
fun <T> C.f1(t: T) = TODO()
val f1Ref1 = C::f1<T> // Compiler error
val f1Ref2 = C::f1 // Compiler error
Run Code Online (Sandbox Code Playgroud)
但这很好:
fun C.f2(i: Int) = TODO()
val f2Ref = C::f2
Run Code Online (Sandbox Code Playgroud) 问题:
我有一个ReactiveCrudRepository我想在 a 中使用的,RestController但 Spring 没有发现它再被注入。在我将存储库重构为响应式CrudRepository之前(它是之前的),存储库是由 Spring 找到并注入的。
现在我收到这个错误:
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of constructor in de.shinythings.microservices.core.product.services.ProductServiceImpl required a bean of type 'de.shinythings.microservices.core.product.persistence.ProductRepository' that could not be found.
Action:
Consider defining a bean of type 'de.shinythings.microservices.core.product.persistence.ProductRepository' in your configuration.
Run Code Online (Sandbox Code Playgroud)
存储库如下所示: 链接到 GitHub
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of constructor in de.shinythings.microservices.core.product.services.ProductServiceImpl required a bean of type 'de.shinythings.microservices.core.product.persistence.ProductRepository' that could not be found.
Action:
Consider defining …Run Code Online (Sandbox Code Playgroud) spring dependency-injection reactive-programming kotlin spring-boot
我在 Kotlin 中看到了很多例子,其中活动类有一个伴随对象来封装启动意图的创建,如下所示。似乎特别受 Java 启发。
class HomeActivity : AppCompatActivity() {
companion object {
fun newStartIntent(context: Context): Intent {
val intent = Intent(context, HomeActivity::class.java)
return intent
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
this.setContentView(R.layout.activity_home)
// ...
}
// ...
}
Run Code Online (Sandbox Code Playgroud)
既然 Kotlin 有顶级函数,为什么不跳过伴随对象而只拥有一个顶级函数呢?
fun newHomeActivityStartIntent(context: Context): Intent {
val intent = Intent(context, HomeActivity::class.java)
return intent
}
class HomeActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
this.setContentView(R.layout.activity_home)
// ...
}
// ...
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试为辅助 Firebase 应用订阅 FCM(Firebase 云消息传递)主题,根据文档,这可以通过getInstance将辅助 FirebaseApp 实例作为参数的重载来完成:
public static synchronized FirebaseMessaging getInstance (FirebaseApp app)
Run Code Online (Sandbox Code Playgroud)
获取指定 FirebaseApp 的 FirebaseMessaging 实例。
我正在使用 Kotlin,我正在build.gradle像这样拉入包:
implementation "com.google.firebase:firebase-messaging:20.2.0"
Run Code Online (Sandbox Code Playgroud)
但是当我尝试FirebaseMessaging使用重载的实例化 时getInstance,我收到一个错误,指出它不可访问。当我查看包源时,反编译表明重载的构造函数不像无参数的那样是公开的getInstance:
implementation "com.google.firebase:firebase-messaging:20.2.0"
Run Code Online (Sandbox Code Playgroud)
我错过了什么?