首页 \ 问答 \ equals()方法?(equals() method?)

equals()方法?(equals() method?)

不应该让一个物体平等吗?

    String hej = pets.getBark();
    if(hej.equals("woff"))

为什么你能传递一个字符串woff?


shouldn´t one pass an object to equal?

    String hej = pets.getBark();
    if(hej.equals("woff"))

why are you able to pass a string woff?


原文:https://stackoverflow.com/questions/2046680
更新时间:2023-10-24 08:10

最满意答案

更新通过不需要任何特定的哈希来简化[1]

更新泛化为通用SimpleShuffle扩展方法

        public static IEnumerable<T> SimpleShuffle<T>(this IEnumerable<T> sequence)
        {
            var rand = new Random();
            return sequence.Select(i => new {i, k=rand.Next()})
                .OrderBy(p => p.k)
                .Select(p => p.i);
        }

我虽然除了downvoting(大喊?对不起:))Anx的答案我认为这将是更好的也显示我的代码将看起来像:

using System;
using System.Linq;
using System.Collections.Generic;

namespace NS
{
    static class Program
    {
        public static IEnumerable<T> SimpleShuffle<T>(this IEnumerable<T> sequence)
        {
            var rand = new Random();
            return sequence.Select(i => new {i, k=rand.Next()}).OrderBy(p => p.k).Select(p => p.i);
        }

        public static void Main(string[] args)
        {
            var pts = from x in Enumerable.Range(0, 24)
                from y in Enumerable.Range(0, 11)
                select new { x, y };

            foreach (var pt in pts.SimpleShuffle())
                Console.WriteLine("{0},{1}", pt.x, pt.y);
        }
    }
}

我完全解决了我以前的问题:如何通过意识到我们不需要散列来产生一个好的散列,除非:

  • 一个。 源包含(逻辑)重复项
  • 湾 我们需要那些具有相同的排序顺序
  • C。 我们希望每次都有相同的“随机”排序顺序(确定性哈希)

一个。 和b。 在这种情况下是错误的,并且c。 甚至会成为一个问题(取决于OP所要求的)。 所以现在,没有任何附加条件,不再担心表现(即使是非理性的担忧),

祝你好运!

[1]顺便说一句,这使整个事情更加灵活,因为我不再需要coords被表示为一个byte []; 你现在可以洗牌你想要的任何结构。


Update Simplified by not requiring any specific hashing [1]

Update Generalzed into generic SimpleShuffle extension method

        public static IEnumerable<T> SimpleShuffle<T>(this IEnumerable<T> sequence)
        {
            var rand = new Random();
            return sequence.Select(i => new {i, k=rand.Next()})
                .OrderBy(p => p.k)
                .Select(p => p.i);
        }

I though in addition to downvoting (shouting? sorry :)) Anx's answer I thought it'd be nicer to also show what my code would look like:

using System;
using System.Linq;
using System.Collections.Generic;

namespace NS
{
    static class Program
    {
        public static IEnumerable<T> SimpleShuffle<T>(this IEnumerable<T> sequence)
        {
            var rand = new Random();
            return sequence.Select(i => new {i, k=rand.Next()}).OrderBy(p => p.k).Select(p => p.i);
        }

        public static void Main(string[] args)
        {
            var pts = from x in Enumerable.Range(0, 24)
                from y in Enumerable.Range(0, 11)
                select new { x, y };

            foreach (var pt in pts.SimpleShuffle())
                Console.WriteLine("{0},{1}", pt.x, pt.y);
        }
    }
}

I totally fixed my earlier problem of how to generate a good hash by realizing that we don't need a hash unless:

  • a. the source contains (logical) duplicates
  • b. and we need those to have equivalent sort order
  • c. and we want to have the same 'random' sort order (deterministic hashing) each time round

a. and b. are false in this case and c. was even going to be a problem (depending on what the OP was requiring). So now, without any strings attached, no more worries about performance (even the irrational worries),

Good luck!

[1] Incidentally this makes the whole thing more flexible because I no longer require the coords to be expressed a byte[]; you can now shuffle any structure you want.

相关问答

更多
  • 这应该做的伎俩。 (这是一个扩展方法,以便您可以像调用Random对象上的普通Next或NextDouble方法那样调用它)。 public static Int64 NextInt64(this Random rnd) { var buffer = new byte[sizeof(Int64)]; rnd.NextBytes(buffer); return BitConverter.ToInt64(buffer, 0); } 如果你想使用无符号整数,只需将Int64替换为随处可用 ...
  • 我唯一加入埃里克的回应是一个解释; 我觉得为什么代码能够工作的知识比知道代码的工作更好。 解释是:假设你想要一个介于2.5到4.5之间的数字。 范围是2.0(4.5 - 2.5)。 NextDouble只返回一个介于0和1.0之间的数字,但如果你乘以这个范围,你将得到一个介于0和范围之间的数字。 所以,这会给我们0.0到2.0之间的随机双打: rng.NextDouble() * 2.0 但是,我们希望他们在2.5到4.5之间! 我们如何做到这一点? 添加最小的数字,2.5: 2.5 + rng.NextD ...
  • 如果您需要读取超过500 MB的时间,那么可以考虑一次将文件流式传输到内存中。 在极端情况下这非常困难,您可能需要考虑其他更容易流式传输的文件格式。 琐碎的例子是mp3和大多数(如果不是全部)视频格式。 如果文件是自定义格式,您可能需要修改格式,以便可以流式传输或至少更容易和更可靠。 但这是一个完全不同的问题。 此外,某些文件系统不能支持超过1 GB,其他文件系统则支持4 GB。 现代FS(NTFS和当前的Linux FS)具有更高的限制。 如果您要求更多信息,那么SO上的某人肯定会告诉您所有的细节。 If ...
  • 有一本书“ .NET环境中的Perl编程 ”可以帮助你。 ActiveState在网上似乎没有任何可公开访问的文档,它提供了有关PerlNET的任何详细信息。 但是如果Perl开发工具包中没有包含某些文档,我会非常惊讶,尽管听起来他们不再提供任何支持。 您可以在perl.net@listserv.activestate.com邮件列表或其存档中找到您的问题的答案。 There's a book "Programming Perl in the .NET Environment" that may help ...
  • private Random gen = new Random(); DateTime RandomDay() { DateTime start = new DateTime(1995, 1, 1); int range = (DateTime.Today - start).Days; return start.AddDays(gen.Next(range)); } 为了获得更好的性能,如果这将重复调用,请在函数外部创建start和生成(甚至是range )变 ...
  • 你需要Random.Next()的这个重载: public virtual int Next( int minValue, int maxValue ) 哪里: minValue =返回的随机数的包含下限。 maxValue =返回的随机数的唯一上限。 maxValue必须大于或等于minValue。 请注意参数说明中包含和排除的单词。 这意味着可以在可能的值中返回最小值,而最大值将不可能。 在返回值的描述中进一步阐明了这一点: 返回值 - 大于或等于minValue且小于maxValue ...
  • 更新通过不需要任何特定的哈希来简化[1] 更新泛化为通用SimpleShuffle扩展方法 public static IEnumerable SimpleShuffle(this IEnumerable sequence) { var rand = new Random(); return sequence.Select(i => new {i, k=rand.Next()}) ...
  • 试试这个,因为Random不是线程安全的: private static Random RANDOM = new Random(); private static object _randomLock = new object(); ... int[] array; // is initialized, when used. ;) if (array.Sum() != 0) { int j = 0; do { lock(_randomLock) { ...
  • 这不是太难。 我没有代码可以使用所以我假设你有对象Move一个属性在一个数组内的权重和所有权重总和达到100.0(并不重要)。 现在你按权重降序对数组进行排序,选择一个0到99之间的随机数,并迭代所有这些减少你的随机数。 一旦它不再是正面,你就停止并选择当前的指数/移动 var value = rnd.NextDouble()*100.0; foreach(var move in moves.OrderByDescending(m => m.Weight)) { value -= move.Weigh ...
  • 您只需要为纹理调用一次Random.Next 。 目前,您正在为每次绘制操作调用它。 You need to make the calls to Random.Next only once for your texture. Currently, you are calling it for every draw operation.

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。