这是在我处理"Cracking the Coding interview"问题时发生的:
编写一个函数来交换一个数字(即没有临时变量)
我决定用Java编写我的解决方案(因为我计划在实习面试中使用Java.)
我提出了一个解决方案,我几乎确信这是正确的答案(因为我在一行中做到了):
public static void main(String args[]) {
int a = 5;
int b = 7;
a = b - a + (b = a);
System.out.println("a: " + a + " b: " + b);
}
Run Code Online (Sandbox Code Playgroud)
当然,这段代码可以执行所需的结果.a == 7和b == 5.
现在这是有趣的部分.
这段代码不会在C++中运行,也不是本书后面的解决方案.
所以我的问题是:为什么我的解决方案确实有效?我假设Java的做法与其他语言有所不同?
所以,我一直在创建一个利用蓝牙和WiFi(用户决定)的无线鼠标应用程序,我最近决定从UDP连接到TCP连接,因为我注意到其中一个主流鼠标应用程序使用TCP而不是UDP.
我的问题:我通过TCP-IP将多个字节数组发送到我的服务器端,但感觉好像存在延迟,有没有什么方法可以加快我接收字节数组的速度?
服务器代码涉及接收:
Socket client = null;
BufferedInputStream bis = null;
try {
client = serverSocket.accept();
} catch (IOException e) {
e.printStackTrace();
}
try {
bis = new BufferedInputStream(client.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
byte data[] = new byte[2];
if (bis != null) {
try {
while(alive && (bis.read(data)) != -1) {
System.out.println(data[0] + " " + data[1]);
PointerInfo a = MouseInfo.getPointerInfo();
Point b = a.getLocation();
int x = (int)b.getX();
int y = (int)b.getY();
dx = data[0]; …Run Code Online (Sandbox Code Playgroud) 我知道我在日志猫中获得的异常有一个非常严格的名字.我唯一的问题是我无法确定导致我的异常的原因.我创建了一个PC-Android多客户聊天.
PC版本与服务器和客户端100%一起工作.我实现了从PC客户端到Android客户端和聊天工作相同的技术.
我得到的唯一问题是,当我连接android,clientArea(跟踪所有连接的用户)时,不跟踪其他名称,而是跟踪android客户端用户名的倍数.即.我只使用用户名:Jurko连接到android,clientArea将有2个人称为Jurko而不是只有一个.
logcat的:
10-11 17:19:08.221: ERROR/AndroidRuntime(12379): FATAL EXCEPTION: main
java.lang.ArrayIndexOutOfBoundsException: length=2; index=2
at com.example.JurkoAndroidChat.MyActivity$ServerTask.onProgressUpdate(MyActivity.java:175)
at com.example.JurkoAndroidChat.MyActivity$ServerTask.onProgressUpdate(MyActivity.java:130)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:647)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4895)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
at dalvik.system.NativeStart.main(Native Method)
Run Code Online (Sandbox Code Playgroud)
Android客户端:
package com.example.JurkoAndroidChat;
import android.app.Activity;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.view.View;
import java.net.*;
import java.io.*;
import java.util.*;
public class MyActivity extends Activity {
/**
* Called when the activity is first created.
*/ …Run Code Online (Sandbox Code Playgroud) java sockets android android-asynctask indexoutofboundsexception