如何获得时区列表中的iOS,就像我们上Web应用程序的注册时,如显示在此PIC?
首先,我想说我在重构方面没有太多经验,也不知道这是否是偏离主题的.
我正在使用给定代码,其中使用了许多布尔属性,由于可读性,我想避免使用它,并且我不知道如何以正确的方式处理它.
class MyClass(){
boolean a;
...
void function(){
...
a = true;
...
}
void anotherFunction(){
...
if(a == true){
//do something
} else {
//do something else
}
...
}
...
}
Run Code Online (Sandbox Code Playgroud)
将boolean a在多个使用function人就像这样,在anotherFunctions的运行有关的代码a.由于使用了多个属性和局部变量,因此很难理解代码和依赖关系,而且我很难重构它anotherFunction.重构这一点的努力可能非常高.
我一直试图避免使用像这样的布尔值,因为在我看来这不是一个好习惯.如果我错了,请不要犹豫,纠正我.
现在我想知道我是否应该重构代码并花费精力?在这种情况下是否可以使用某种模式?
我正在开发一个使用goftp上传到服务器的项目,但是(感谢这里的善意的人)我将使用更安全的方法.
我打算使用ssh来代替,发现golang这个SSH客户端找到这里.
我已经设置了一个ssh服务器(freeSSHd),并且可以在本地和另一台机器上成功连接PuTTY.
我只更改了客户端的这一部分,用我自己的变量替换变量
var (
server = "127.0.0.1:22"
username = "username"
password = clientPassword("password")
)
Run Code Online (Sandbox Code Playgroud)
当我执行ssh客户端时,ssh.Dial返回错误,并且恐慌显示: "无法拨号:握手失败:ssh:没有常见算法"
client, err := ssh.Dial("tcp", "127.0.0.1:22", config)
if err != nil {
panic("Failed to dial: " + err.Error())
}
Run Code Online (Sandbox Code Playgroud)
我是golang的新手,所以我很感激任何帮助,指出我正确的方向.提前致谢.
我无法找到ThreadA仅在ThreadB完成它的工作时才从ThreadB获取通知的原因.如果我发表评论notify()无论如何都会获得通知.
ThreadB完成它的工作后会自动发送通知吗?
为什么ThreadB没有按时通知ThreadA?
class ThreadA {
public static void main(String [] args) {
ThreadB b = new ThreadB();
b.start();
synchronized(b)
{
try {
System.out.println("Waiting for b to complete...");
b.wait();
System.out.println("waiting done");
} catch (InterruptedException e) {}
System.out.println("Total is: " + b.total);
}
}
}
class ThreadB extends Thread {
int total=0;
public void run() {
synchronized(this)
{
for(int i=0;i<10;i++)
{
System.out.println(Thread.currentThread().getName()+" "+ Integer.toString(i));
total += i;
}
notify();
System.out.println("notify done");
for(int i=0;i<100;i++)
{
System.out.println(Thread.currentThread().getName()+" "+ Integer.toString(i));
total += i;
} …Run Code Online (Sandbox Code Playgroud) 我正在使用Mockito测试这样的方法:
myMethod(String s, List<Integer> i);
Run Code Online (Sandbox Code Playgroud)
我用这个电话:
doReturn(foo).when(bar).myMethod(anyString(), isNull(List.class))
Run Code Online (Sandbox Code Playgroud)
使用该isNull(Class<?> class)方法是我的问题。因为我使用的是Matcher的anyString(),所以我不能仅仅通过null。
现在type safety,由于使用了通用类,我得到一个警告。我可以使用@SuppressWarning注释来摆脱它。
这个问题比Mockito与Java有关,但我想提供用例,在这里我不能仅仅使用List的实例将其传递给方法。
我首先想知道的是一种摆脱警告的干净方法。由于强制转换(泛型,类型为橡皮擦)不是选项。
我有一些Fragments在List Collection被用于Viewpager具有FragmentStatePagerAdapter.我只使用getItem(){return list.get(index;}和getCount(){return list.size()}在其中.如果我想在适配器中替换该List,我加载另一个具有不同片段的List,调用notifyDataSetChanged()并再次将适配器设置到viewpager以实现它.那很好用.
但是,如果我执行此片段集更改,之前的片段将onDestroy()通过PagerAdapter 调用而被破坏.现在我想知道是否有可能阻止onDestroy调用?或者我必须使用onSavedInstanceState()再次加载以前的碎片及其状态?
我只是搜索一些不同的方法来恢复由于适配器的变化而消失的片段的状态.如果有人对此有所了解,分享一些想法会很高兴.如果必要,我可以提供代码.
也许是一个很好的信息:我不使用PagerAdapter来实例化片段.我在将列表加载到适配器之前执行此操作.
我有主片段,我想传递ArrayList给Activity类,我将在ListView中显示结果.
片段类:
public class StudentActivity extends Fragment implements OnClickListener {
}
Run Code Online (Sandbox Code Playgroud)
我有数据
ArrayList<> allStudents = new ArrayList();
allStudents.add(new Student("H", 99, 93) );
allStudents.add(new Student("C", 98, 92) );
allStudents.add(new Student("B", 98, 91) );
allStudents.add(new Student("A", 80, 94) );
allStudents.add(new Student("F", 70, 84) );
Run Code Online (Sandbox Code Playgroud)
现在我想将"allStudents"对象发送到新的活动类StudentResult();
我在片段类中使用:
Intent intent = new Intent(getActivity(), StudentResult.class);
intent.putExtra("ExtraData", allStudents);
startActivity(intent);
Run Code Online (Sandbox Code Playgroud)
并在目标类中显示ListView()中的对象;
public class ResultActivity extends Activity {
public void myData(ArrayList<allStudents> myArray) {
marjslistview = (ListView) findViewById(R.id.listView1);
arrayAdapter = new ArrayAdapter<allStudents>(this, R.layout.student_list, myArray);
... …Run Code Online (Sandbox Code Playgroud) 示例下面的程序,其中在try块中调用了defectedCode()方法,那么为什么只输出只显示C带有"线程中的异常"主"java.lang.Error".
public class ExceptionTest {
public static void defectedCode(){
throw new Error();
}
public static void main(String args[]){
try{
defectedCode();
System.out.println("A");
}catch(Exception e){
System.out.println("B");
}finally{
System.out.println("C");
}
System.out.print("D");
}
}
Run Code Online (Sandbox Code Playgroud)
Exception in thread "main" java.lang.Error
C
at ExceptionTest.defectedCode(ExceptionTest.java:15)
at ExceptionTest.main(ExceptionTest.java:21)
Java Result: 1
Run Code Online (Sandbox Code Playgroud) 我正在编写一个程序,以String数组(从用户输入)到文件的形式写出一组观察结果.我能够对.txt文件进行观察,然后在不删除先前数据的情况下添加新观察,但我的所有数据都在同一行.我需要将每组观察结果分开.此外,我需要以后能够访问该文件并从中读取.
我的代码目前看起来像这样:(观察是字符串数组)
for(int i = 0; i < observation.length; i++) {
try (BufferedWriter bw = new BufferedWriter(new FileWriter("birdobservations.txt", true))) {
String s;
s = observation[i];
bw.write(s);
bw.flush();
} catch(IOException ex) {}
}
Run Code Online (Sandbox Code Playgroud)
我的输出目前看起来像这样: CrowMOsloMay2015JayMOsloJune2012CrowMOsloMay2015RobinFBergenMay2012
我希望它看起来像这样:
Crow M Oslo May2015
Jay M Oslo June2012
Run Code Online (Sandbox Code Playgroud)
...等等
我该怎么做呢?我假设我需要某种循环,但我现在已经停留了一段时间.
关于以下片段的一些解释:
我正在搞乱一些蓝牙发现电话.为此,我正在使用回调,如果BluetoothDevice找到a,将调用该回调.如果未找到设备,则参数为null:
@Override
public void provideDevice(BluetoothDevice device) {
super.provideDevice(device);
Log.v("MainActivity","device name = " +device.getName());
if(device != null) {
mBinder.start(device);
} else {
Toast.makeText(this, "No Device found", Toast.LENGTH_SHORT).show();
}
}
Run Code Online (Sandbox Code Playgroud)
Eclipse告诉我else块是死代码.
如果我Log在if-block中移动调用,则警告消失:
@Override
public void provideDevice(BluetoothDevice device) {
super.provideDevice(device);
if(device != null) {
Log.v("MainActivity","device name = " +device.getName());
mBinder.start(bc);
} else {
Toast.makeText(this, "No Device found", Toast.LENGTH_SHORT).show();
}
}
Run Code Online (Sandbox Code Playgroud)
我知道如果参数为null,第一个片段将抛出NPE.这不是本例中的问题.
我想知道为什么会dead code warning出现.
我可以提供完整的代码,如果这还不足以告诉我发生了什么.