小编Try*_*ard的帖子

有人可以解释这两个循环之间的区别吗?

   while(n>= 0 && nums[n] == value){  
        n--;
    }

   while(n>=0 && nums[n--] == value){
   }
Run Code Online (Sandbox Code Playgroud)

我认为这两个循环应该完全相同.发生的是我运​​行测试用例输入[4,5]值= 4.并获得一个超出边界的数组索引.但是,如果我可以进入另一个循环,它就可以了.

public int removeElement(int[] nums, int val) {
    if(nums.length == 0) return 0;
    // use two pointers, n for the right most of the item, which value != val. 
    // i for the start pointer. 
    int n = nums.length-1;
    int i = 0;
    while(n>= 0 && nums[n--] == val){ 
    }

    while(i <= n){
        // as long as the curr items is equal to the valu …
Run Code Online (Sandbox Code Playgroud)

java debugging loops

1
推荐指数
1
解决办法
112
查看次数

使用for循环创建变量

我有一个包含四个不同位置的String,用逗号分隔.我将长字符串拆分为四个部分并将位置分配给location变量.目前这很简单(见下文).

int noOfLocations = counter + 1;

//Splits the long input location string into the four locations.
String[] locations = inputLocation.split(",");
String location1 = locations[0];
String location2 = locations[1];
String location3 = locations[2];
String location4 = locations[3];
Run Code Online (Sandbox Code Playgroud)

但是,我希望能够输入任意数量的位置,并为每个使用for循环分配一个变量.

我觉得这是一件相当简单的事情,但我似乎无法正确编码.我有一个计数器,它计算逗号的数量,并在该数字上加一个以查找位置数.

基本上我想说的是:

String[] locations = inputLocation.split(",");
for i in range noOfLocations {
    String location(i) = locations[i];
}
Run Code Online (Sandbox Code Playgroud)

写这样的东西的正确语法是什么?

java string loops for-loop

1
推荐指数
1
解决办法
242
查看次数

如何在C中读取和分配大整数?

我试图将大整数值分配给c中的变量,当我打印时,我只得到10123456.

有什么问题?

  int main(){
      long a = 1234567890123456;
      printf("\n",sizeof(a));
      printf("%ld",a); 
  }
Run Code Online (Sandbox Code Playgroud)

c

0
推荐指数
1
解决办法
630
查看次数

在不使用C中的第三个变量的情况下交换两个变量的值?

我找到了以下代码片段:

#include <stdio.h>

int main(void) {
    int x=10,y=15;
    x=x+y-(y=x);
    printf("x=%d y=%d",x,y);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

它实际上交换了变量

任何人都可以解释一下代码如何交换变量?

我认为括号将首先执行然后表达式导致

x=x+y-y;
Run Code Online (Sandbox Code Playgroud)

c expression operator-precedence sequence-points

0
推荐指数
1
解决办法
175
查看次数

在Android中检查互联网连接的最佳方法是什么?

我正在使用这些代码AsyncTask检查Internet连接:

ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo netInfo = cm.getActiveNetworkInfo();
            if (netInfo != null && netInfo.isConnected()) {
                try {
                    URL url = new URL("http://www.google.com");
                    HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
                    urlc.setConnectTimeout(3000);
                    urlc.connect();
                    if (urlc.getResponseCode() == 200) {
                        return true;
                    }
                } catch (MalformedURLException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            else {
    //toast show
            }
Run Code Online (Sandbox Code Playgroud)

此代码不适用于大多数华为型号和三星Galaxy Note 3等设备.

此外,如果用户与GPRS,EDGE,3G,4G等数据进行互联网连接...

在所有设备和所有类型的连接上检查互联网连接以支持的最佳代码是什么?

android

0
推荐指数
1
解决办法
1261
查看次数

printf%e科学记数法在C中给出错误的值

我正在做一个非常基本的科学记数法打印输出.

但是,在我看来它不起作用,我无法弄清楚为什么?

码:

size_t result = 0;
printf("%e \n",result);
Run Code Online (Sandbox Code Playgroud)

打印的价值如下:

1.278341e-307
Run Code Online (Sandbox Code Playgroud)

做科学记数法的正确方法是什么?

c printf scientific-notation

0
推荐指数
1
解决办法
516
查看次数

i ++和++ i之间的区别?

这是我的代码:

int main()
{
    int i=2,j=2,c=2,d;

    d= ++i || ++j && c;
    printf("I is %d\n",i);
    printf("J is %d\n",j);
    printf("C is %d\n",c);
    printf("D is %d\n",d);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

以下代码的输出是:

i is 3                                                                                                                                                                          
j is 2                                                                                                                                                                          
c is 2                                                                                                                                                                          
d is 1
Run Code Online (Sandbox Code Playgroud)

现在我的问题是,如果++i是,3那为什么++j是2?

++ii++?有什么区别?此外,我想知道是怎么来d1

c short-circuiting

-2
推荐指数
2
解决办法
467
查看次数