Java中函数指针的使用方法
来源:Dev.to 指针是存储内存地址的对象,可以通过直接指向目标对象、数组或变量地址而不是按值传递来节省内存。不幸的是,Java 中没有“真正”的指针概念。但幸运的是,我们有一种使用接近真实情况的方法引用的解决方法。函数指针
函数指针是指向函数地址的指针。一些用例包括通过创建一个根据其执行情况调用另一个函数的函数来创建回调例程,或者为动态调用的方法存储函数指针数组(例如,存储模拟器的处理器指令)。函数指针模拟
方法引用有四种类型。我们使用引用特定对象的实例方法的类型。让我们首先定义一个接口,该接口定义您指向的方法的签名。// Wrapping interface
private interface FunctionPointer {
// Method signatures of pointed method
void methodSignature(int a);
}
然后我们将创建具有目标方法签名的方法。
public void method1(int b) {
System.out.println("Called method1 with integer " + b);
}
public void method2(int v) {
System.out.println("Called method2 with integer " + v);
}
public void method3(int a) {
System.out.println("Called method3 with integer " + a);
}
下一步是创建包装器接口变量并为其分配方法。变量将充当指向要存储或执行的函数的指针。
// Create a variable of the interface and assign
// the method references
FunctionPointer pointer1 = this::method1;
FunctionPointer pointer2 = this::method2;
// Call both methods using their "pointer"
pointer1.methodSignature(3);
pointer2.methodSignature(2);
// Reassign and call pointer 1
pointer1 = this::method3;
pointer1.methodSignature(5);
使用整数 3 调用方法 1 使用整数 2 调用方法 2 使用整数 5 调用方法 3
使用 Lambda 表达式
可以使用 lambda 表达式分配方法引用。// Create a method reference and assign a methods using a lambda.
FunctionPointer pointer1 =
(a) -> System.out.println("Called pointer1 with int " + a);
FunctionPointer pointer2 =
(b) -> System.out.println("Called pointer2 with int " + b);
函数指针数组
可以通过创建包装器接口数组来模拟方法引用数组的功能。// Create an array of "pointers"
FunctionPointer[] functionPointersArray = new FunctionPointer[3];
// Assign methods
functionPointersArray[0] = this::method1;
functionPointersArray[1] = this::method2;
functionPointersArray[2] = this::method3;
// Call methods
functionPointersArray[0].methodSignature(3);
functionPointersArray[1].methodSignature(4);
functionPointersArray[2].methodSignature(5);
函数指针与直接调用
如果比较这两个选项,直接调用方法几乎比使用方法引用快 5 倍。这都是因为与存储方法相比,调用 lambda 表达式需要额外的步骤。但一年内您可能不会注意到任何性能损失,因此不必担心。结论
指针是直接指向对象地址而不是值的变量。函数指针直接指向函数地址,可以减少内存消耗。Java 没有指针,但可以使用方法引用或 lambda 表达式来模拟行为。使用方法引用比直接调用方法慢,但这并不妨碍它的使用。Java 中的 indexOf - 如何在 Java 中查找字符串的索引
来源:FreeCodeCamp 字符串是用双引号括起来的一组字符。indexOf方法返回字符串中指定字符或子字符串的索引位置。在本文中,我们将了解各种indexOf方法的语法。我们还将查看一些示例,帮助您理解并有效地使用在 Java 代码中搜索字符或子字符串的索引。indexOf 方法的语法
indexOf方法有以下方法:public int indexOf(int char)
public int indexOf(int char, int fromIndex)
public int indexOf(String str)
public int indexOf(String str, int fromIndex)
我们来解释一下这些参数:
- char代表每行一个字符。
- fromIndex指定搜索字符或子字符串索引的开始位置。当字符串中有两个具有相同值的字符/字符串时,这一点很重要。通过这个参数,你可以告诉indexOf方法从哪里开始。
- str表示字符串中的子字符串。
如何在Java中使用indexOf方法
在第一个示例中,我们将查找字符串中一个字符的索引。这将有助于我们理解public int indexOf(int char)方法。IndexOf(int Char) 方法示例
public class Main {
public static void main(String[] args) {
String greetings = "Hello World";
System.out.println(greetings.indexOf("o"));
// 4
}
}
在上面的代码中,我们得到了返回给我们的字符“o”的索引,即4。我们有两个字符“o”,但只返回了第一个字符的索引。在下面的示例中,我们将看到如何返回第二个“o”的索引。如果您想知道如何获取序列号,您会注意到字符串中的第一个字符的索引为零,第二个字符的索引为 1,依此类推。
indexOf(int Char, Int fromIndex) 方法的示例
这是解释int indexOf(int char, int fromIndex)方法的示例:public class Main {
public static void main(String[] args) {
String greetings = "Hello World";
System.out.println(greetings.indexOf("o", 5));
// 7
}
}
在上面的示例中,我们告诉indexOf方法从第五个索引开始工作。H => 索引 0 e => 索引 1 l => 索引 2 l => 索引 3 0 => 索引 4 请注意,索引 5 不是“W”字符。第五个索引是“Hello”和“World”之间的空格。因此,在此代码中,第五个索引之前的所有其他字符都将被忽略。7 作为第二个字符“o”的索引返回。
Int indexOf(String Str) 方法的示例
在下面的示例中,我们将了解public int indexOf(String str)方法的工作原理,该方法返回子字符串的索引。public class Main {
public static void main(String[] args) {
String motivation = "Coding can be difficult but don't give up";
System.out.println(motivation.indexOf("be"));
// 11
}
}
我想知道我们是怎么拿回11的?您应该查看最后一节以了解如何计算索引以及子字符串之间的空格如何也被视为索引。请注意,当子字符串作为参数传递时,返回的索引是子字符串中第一个字符的索引。11是字符“b”的索引。
indexOf(String Str, Int fromIndex) 方法示例
最后一个方法是public int indexOf(String str, int fromIndex) - 与public int indexOf(int char, int fromIndex)方法相同。它返回指定位置的索引。public class Main {
public static void main(String[] args) {
String motivation = "The for loop is used for the following";
System.out.println(motivation.indexOf("for", 5));
// 21
}
}
在此示例中,我们指定该方法应从第五个索引开始工作,该索引位于第一个子字符串 for 之后。21是for的第二个子串的索引。最后,当我们传递一个不在字符串中的字符或子字符串时,indexOf方法将返回-1。这是一个例子:
public class Main {
public static void main(String[] args) {
String motivation = "The for loop is used for the following";
System.out.println(motivation.indexOf("code"));
// -1
}
}
GO TO FULL VERSION