我正在尝试使用lambdas,Java
但无法理解它是如何工作的.我这样创建@FunctionalInterface
:
@FunctionalInterface
public interface MyFunctionalInterface {
String getString(String s);
}
Run Code Online (Sandbox Code Playgroud)
现在在我的代码中我使用lambda
as在这里:
MyFunctionalInterface function = (f) -> {
Date d = new Date();
return d.toString() + " " + person.name + " used fnc str";
};
Run Code Online (Sandbox Code Playgroud)
接下来,我想利用我function
将它传递给另一个类的构造函数并像这样使用它:
public SampleClass(MyFunctionalInterface function) {
String tmp = "The person info: %s";
this.result = String.format(tmp, function.getString(String.valueOf(function)));
}
Run Code Online (Sandbox Code Playgroud)
为什么我需要在valueOf()
这里使用它?我认为,谢谢你,我可以使用function.getString()
吗?
输出:
Tue Sep 19 11:04:48 CEST 2017 John used fnc str
我已将格式化我的字符串看起来像JSON
我可以做到的json.loads
.当我在屏幕上打印时,它结果搞砸了订单.我知道Python的dictonaries没有订购,但有没有办法保持这个顺序?我真的需要保留它.谢谢!
所以,在我的应用程序中,我将请求发送http server handler
到另一台服务器.事情是第二个服务器卡在阅读对象上.我试图弄清楚自己,但我看不出问题.所有流都关闭了,也许对客户的响应是在错误的地方?我不知道为什么会这样..
这是我的客户:
public class ClientSimulator {
private Random random;
private static int clientCounter = 1;
public static void main(String[] args) throws Exception {
new ClientSimulator();
new ClientSimulator();
new ClientSimulator();
}
private ClientSimulator() {
this.random = new Random();
RuntimeMXBean rmb = ManagementFactory.getRuntimeMXBean();
long arrivalTime = rmb.getUptime();
System.out.println("thread no. " + clientCounter++ + " arrival time: " + arrivalTime);
try {
String myurl= "http://localhost:8080/sender";
String serverResponse = createClient(myurl);
System.out.println(serverResponse);
} catch (Exception e) {
e.printStackTrace();
}
}
private …
Run Code Online (Sandbox Code Playgroud) 问题是:如何(以编程方式)确定插入 USB 闪存驱动器时使用了哪个 USB 端口?
语境:
我正在为 Android 电视盒开发一个应用程序,让您可以查看 Pendrive 中的文件。我的设备有 3 个 USB 端口。我想知道使用了哪个端口(某种 ID、端口名称,任何可以帮助我在代码中识别它的内容)。
我做了什么:
deviceName
是一个字符串,包含在 Android 系统中创建的设备文件的路径,但每次插入 USB 闪存驱动器时它都会发生变化(看起来像这样/dev/bus/usb/007/008
,最终的数字每次都不同)。adb shell
,但找不到任何描述 USB 端口并说明当前正在使用哪个端口的信息。也许有人遇到过类似的问题?我真的很感激任何帮助。提前致谢!
我正在使用此库:https://bintray.com/google/webrtc/google-webrtc
我想要实现的目标(至少在我的项目开始时)是本地渲染视频.我正在使用本教程(这是互联网上唯一的一个)https://vivekc.xyz/getting-started-with-webrtc-for-android-daab1e268ff4.不幸的是,最后一行代码不再是最新的.构造函数需要一个回调,我不知道如何实现:
localVideoTrack.addRenderer(new VideoRenderer(i420Frame -> {
// no idea what to put here
}));
我的代码与发布的教程完全相同.这是熟悉Android中的WebRTC技术的第一步,这是我无法弄清楚的.我的相机正在捕捉视频,因为我可以在我的日志中看到它:
I/org.webrtc.Logging: CameraStatistics: Camera fps: 28.
主要问题是我不知道如何SurfaceViewRenderer
通过回调将其传递给我.有没有人遇到这个问题?我真的很感激任何帮助或建议.
这是官方示例应用程序,它是唯一的来源,但它与教程中的不同,它更复杂:https: //webrtc.googlesource.com/src/+/master/examples/androidapp/src/org/Appspot上/ apprtc
我正在学习 Java 中的容器,最近我读到 HashSet 没有按顺序提供元素。Integer有趣的是我随机制作的 HashSet 被排序了。当我将其类型更改为Double 时,打印的 HashSet 不再排序。我的问题是:那么 HashSet 对各种类型的工作方式是否不同?
我正在尝试对我的列表进行排序,但这个不起作用.方法collections.sort()什么都不做.
public boolean schedule(){
List<Task> keys = new ArrayList<Task>(g.tasks.keySet());
for(int i = 0; i<keys.size();i++){
System.out.println(keys.get(i).getSize());
}
Collections.sort(keys);
for(int i = 0; i<keys.size();i++){
System.out.println(keys.get(i).getSize());
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
这是Task类中的compareTo()方法:
public int compareTo(Task t1) {
Integer csize = new Integer(t1.size);
int cmp = csize.compareTo(t1.size);
return cmp;
}
Run Code Online (Sandbox Code Playgroud)
这种方法有什么问题?
我正在尝试编写一个简短的程序,将每个单词放在一个新行上。新行可以通过制表符、空格或回车来确认。程序的结尾是在控制台中输入“#”。我遇到的问题是,当我将“enter”输入控制台时,它会在同一行中写入下一个字符。
第二个想法是将所有这些都放在一个表格中,这样我最后就可以将格式化文本放在一起。这个我也想不通。
#include<stdio.h>
#include <conio.h>
#define STOP '#'
int main()
{
char ch;
while ((ch = (_getch())) != STOP) {
switch (ch) {
case '\n':
printf("\n");
break;
case '\t':
printf("\n");
break;
case ' ':
printf("\n");
break;
default:
putchar(ch);
}
}
printf("\nEND");
_getch();
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我目前正在学习如何实现JUnit测试,并且我正在为我的Stack实现使用isEmpty()方法.这是我想测试的方法:
boolean isEmpty(){
if(firstFree == 0) return true;
else return false;
}
Run Code Online (Sandbox Code Playgroud)
这是我的考验:
public void isEmptyTest(){
assertTrue(onpStack.isEmpty());
assertFalse(onpStack.isEmpty());
}
Run Code Online (Sandbox Code Playgroud)
如果堆栈有或没有元素没有区别 - 测试总是失败.我知道为什么:因为它不能同时拥有和没有元素.在这两种情况下,我都无法弄清楚如何通过测试.有没有正确的方法?或者我应该为这个isEmpty()方法进行两个不同的测试?
我有一个数组,其中包含指向其他数组的指针.我想打印所有值,但我无法获得特定数组的sizeof.我究竟做错了什么?
int main(void){
int i, j;
float T1[4]={1.1, 1.2, 1.3, 1.4};
float T2[6]={2.1, 2.2, 2.3, 2.4, 2.5, 2.6};
float T3[3]={3.1, 3.2, 3.3};
float T4[2]={4.1, 4.2};
float T5[4]={5.1, 5.2, 5.3, 5.4};
float *TAB[5]={T1, T2, T3, T4, T5};
for(i=0; i<5; i++){
for(j=0; j<sizeof(TAB[i])/(sizeof (int)); j++){
printf("%f ", *(TAB[i]+j));
}
}
printf("\n");
}
Run Code Online (Sandbox Code Playgroud)