小编Nom*_*Sim的帖子

Golang确定*File是否指向文件或目录

有没有办法确定我的*文件是指向文件还是目录?

fileOrDir, err := os.Open(name)
// How do I know whether I have a file or directory?
Run Code Online (Sandbox Code Playgroud)

我希望能够读取有关该文件的统计信息(如果它只是一个文件),并且能够读取目录中的文件(如果它是一个目录)

fileOrDir.Readdirnames(0) // If dir
os.Stat(name) // If file
Run Code Online (Sandbox Code Playgroud)

file go

34
推荐指数
3
解决办法
3万
查看次数

我可以在Visual Studio中刷新XML注释以反映已更改的参数吗?

如果我写这个函数:

    public static uint FindAUint(double firstParam)
    {

    }
Run Code Online (Sandbox Code Playgroud)

我可以通过输入'///'来生成xml注释,它给出:

    /// <summary>
    /// *Here I type the summary of the method*
    /// </summary>
    /// <param name="firstParam">*Summary of param*</param>
    /// <returns>*Summary of return*</returns>
    public static uint FindAUint(double firstParam)
    {

    }
Run Code Online (Sandbox Code Playgroud)

如果我然后决定我需要更新我的方法:

    /// <summary>
    /// *Here I type the summary of the method*
    /// </summary>
    /// <param name="firstParam">*Summary of param*</param>
    /// <returns>*Summary of return*</returns>
    public static uint FindAUint(double firstParam,double newParam, double newParam2)
    {

    }
Run Code Online (Sandbox Code Playgroud)

有没有办法让visual studio将新的参数添加到xml中而不会丢失之前的描述?

(我应该提到我正在使用Visual Studio Express;我不会过去通过Microsoft来禁止Express版本中的功能)

c# comments visual-studio-2010

27
推荐指数
2
解决办法
6615
查看次数

是否可以在不使用OrderBy的情况下将IEnumerable转换为IOrderedEnumerable?

假设有一种扩展方法可以根据SortMethod枚举指定的几种类型的排序(即按各种属性排序)来订购IQueryable .

public static IOrderedEnumerable<AClass> OrderByX(this IQueryable<AClass> values,
    SortMethod? sortMethod)
{ 
    IOrderedEnumerable<AClass> queryRes = null;
    switch (sortMethod)
    {
        case SortMethod.Method1:
            queryRes = values.OrderBy(a => a.Property1);
            break;
        case SortMethod.Method2:
            queryRes = values.OrderBy(a => a.Property2);
            break;
        case null:
            queryRes = values.OrderBy(a => a.DefaultProperty);
            break;
        default:
            queryRes = values.OrderBy(a => a.DefaultProperty);
            break;
    }
    return queryRes;
}
Run Code Online (Sandbox Code Playgroud)

sortMethodis 的情况下null(即指定我不关心值的顺序),是否有一种方法来代替通过某个默认属性排序,而只是将IEnumerator值传递为"有序"而不是必须执行实际排序?

我希望能够调用此扩展,然后可能执行一些额外的ThenBy排序.

c# linq ienumerable iorderedenumerable

26
推荐指数
2
解决办法
7028
查看次数

有效数字String上的NumberFormatException

我已经看到了一些其他问题,但错误与字符串中的前导0有关.不幸的是,这不是我的情况.

我从base64格式的外部源接收加密数据,然后解码它(使用包含的Base64库,因为android sdk版本是7),解密消息,毕竟我有一个数字格式的简单字符串.

当我尝试将其投射到LongInteger我收到此错误时:

java.lang.NumberFormatException: Invalid long: "2551122"
    at java.lang.Long.invalidLong(Long.java:125)
    at java.lang.Long.parse(Long.java:362)
    at java.lang.Long.parseLong(Long.java:353)
    at java.lang.Long.parseLong(Long.java:319)
    at com.nzn.lol.LoginActivity$LoginTask.doInBackground(LoginActivity.java:98)
    at com.nzn.lol.LoginActivity$LoginTask.doInBackground(LoginActivity.java:1)
    at android.os.AsyncTask$2.call(AsyncTask.java:264)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
Run Code Online (Sandbox Code Playgroud)

要检查输入我使用的打印,它实际上是字符串"2551122".当我尝试检查相等性时,它也是不正确的

"2551122".equals(numberAsString) // Gives me false
Run Code Online (Sandbox Code Playgroud)

我认为这是一个编码问题,并试图采用解码的字节并在几个编码中创建字符串,也试图用相同的几个编码解码base64字符串中的字节,但仍然不知道是什么导致了这个错误.

请任何帮助表示赞赏

UPDATE

这是用于解密字符串的代码(Encryptor类):

private static byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception {
    SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
    Cipher cipher = Cipher.getInstance(encryptionAlgorithim);
    cipher.init(Cipher.DECRYPT_MODE, skeySpec, new IvParameterSpec(iVector));
    byte[] decrypted = cipher.doFinal(encrypted);
    return decrypted;
}

public String decrypt(String encryptedString, String …
Run Code Online (Sandbox Code Playgroud)

java string encryption base64 android

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

编译器会针对布尔赋值进行优化吗?

这就是我想要做的:

if(ABoolean || (BBoolean && CBoolean))
{
    SomeButton.Enabled = true;
    AnotherButton.Enabled = true;
}
else 
{
    SomeButton.Enabled = false;
    AnotherButton.Enabled = false;
}
Run Code Online (Sandbox Code Playgroud)

我可以将其切换为:

SomeButton.Enabled = (ABoolean || (BBoolean && CBoolean));
AnotherButton.Enabled = (ABoolean || (BBoolean && CBoolean));
Run Code Online (Sandbox Code Playgroud)

更简洁的代码.我的问题是,编译器是否优化了赋值,以便它将看到布尔表达式相同并为第二个按钮分配其值,或者它将每次计算值.

注意:我知道这是一个简单的例子,加速/减速将是微不足道的,无法实现,但它将有助于我更好地理解编译器优化.

编辑:这就是我认为可能优化第二个选项的原因:

class Program
{
    static bool ABoolean = true, BBoolean = true, CBoolean = false;
    static bool AEnable, BEnable;


    static void Main(string[] args)
    {
        Stopwatch sw = new Stopwatch();
        sw.Start();
        for (int i = 0; i < 1000000000; i++)
        { …
Run Code Online (Sandbox Code Playgroud)

c# compiler-optimization

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

更优雅的方式将索引更新为循环列表?

我有一个用户将迭代的问题列表,他们可以从任何问题开始,但他们确实有他们的命令,所以为了做到这一点,我只是维护一个索引到数组并增加它如下:

CurrentQuestion = (++CurrentQuestion < questions.Length) ? CurrentQuestion : 0;
Run Code Online (Sandbox Code Playgroud)

这里发生的事情不一定明显,是否有更优雅的方式来做到这一点?

c#

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

UnionBy Linq 实现

我需要 Union 的实现来比较对象的属性,而不是对象本身。我想出了以下几点:

public static IEnumerable<TSource> UnionBy<TSource, TKey>(
    this IEnumerable<TSource> first,
    IEnumerable<TSource> second,
    Func<TSource, TKey> keySelector,
    IEqualityComparer<TKey> keyComparer = null)
{
    HashSet<TKey> keys = new HashSet<TKey>(keyComparer);
    foreach (TSource element in first)
    {
        if (keys.Add(keySelector(element)))
        {
            yield return element;
        }
    }
    foreach (TSource element in second)
    {
        if (!keys.Add(keySelector(element)))
        {
            continue;
        }
        yield return element;
    }
}
Run Code Online (Sandbox Code Playgroud)

我可以通过说以下内容来使用它:

result = first.UnionBy(second, x => x.Property1);
Run Code Online (Sandbox Code Playgroud)

这对我有用,但我想知道 Linq 中是否已经实现了我所缺少的东西(除了实现我自己的 EqualityComparer 之外,这对我来说似乎不太直观)。

因为我不会每次想要这个联合时都使用相同的属性,所以我要么必须EqualityComparer为每种情况创建多个,这对我来说似乎不正确,要么创建一些包含EqualityComparer属性选择器 Func 的泛型。对我来说,这似乎不如仅仅提供一个接受属性选择器本身的通用 Linq 扩展那么直观。

c# linq

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

使用属性与计算更改值的方法

是否存在使用属性来计算通话值的约定?例如,如果我的类包含一个整数列表,并且我有一个属性Average,那么当从列表中添加/删除/修改整数时,平均值可能会发生变化,这样做会是这样的:

    private int? _ave = null;
    public int Average
    {
        get
        {
            if (_ave == null )
            {
                double accum = 0;
                foreach (int i in myList)
                {
                    accum += i;
                }
                _ave = accum / myList.Count;
                return (int)_ave;
            }
            else
            {
                return (int)_ave;
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

如果以可能改变平均值的方式修改myList,则将_ave设置为null

对平均方法调用有任何常规优势/劣势吗?

我基本上只是想知道这个约定是什么,因为我正在创建一个具有可能只计算一次的特定属性的类.我喜欢访问这些属性的类的想法,以便能够访问属性与方法(因为它似乎更可读IMO,将平均值视为属性而不是方法),但我可以看到这里可能会有问题,特别是在确保_ave被适当地设置为null时.

c# coding-style conventions

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

中位数的动态规划

我坚持这个问题,并想知道是否有人可以帮助我:x轴上有n个房子{x_1,x_2,... x_n},我需要在x轴上找到给我的位置房屋和地点之间的最小距离.

这当然是微不足道的,但我也需要能够在O(n)时间内完成它,并且我仍然坚持使用动态算法.

编辑:显然它不需要是一个DP算法,正如我所说的那样让它变得微不足道,对于混乱感到抱歉,并感谢你的回复.

java algorithm dynamic

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

为什么返回的函数数组是输入列表的两倍?

我在python中创建了一个简单的函数:

def func(a,x):
  return a+x*2
Run Code Online (Sandbox Code Playgroud)

然后我称之为

x = [log(1),log(2),log(4),log(5),log(8)]
#Import y data from a file
free= curve_fit(func,np.array(x),np.array(y))[0][0]
yline = func(free,x)
Run Code Online (Sandbox Code Playgroud)

结果yline是两倍长,x并且每个元素在那里两次.

为什么会这样?

注意: 我正在导入numpy但不是scipy或curve_fit

python numpy scipy

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