- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一个有趣的问题
这是对象结构
public class Testdata {
//Which is a consecutive running number i.e 1,2,3..etc
private int sequence;
//classified based on this again any random numbers
private int window;
//need to calculate
private int windowposition;
}
现在基于序列和窗口,我需要推导出相对于窗口的窗口位置
测试数据
所以对于测试数据序列/窗口
1 / 2
2 / 3
3 / 2
4 / 3
5 / 3
预期输出
sequence/window : window position would be (in the same order)
1 / 2 : 1
2 / 3 : 1
3 / 2 : 2
4 / 3 : 2
5 / 3 : 3
更新:
是的,我已经实现了可比性并将列表按以下顺序排序
1 / 2
3 / 2
2 / 3
4 / 3
5 / 3
现在如何计算每个元素相对于其窗口的windowposition
最佳答案
实现 Comparable 可能是有意义的.这允许对您的对象进行排序。您可以像这样实现 compareTo(T)
:
int compareTo(Testdata o) {
return ((Integer)this.sequence).compareTo(o.sequence);
}
这样您的对象就可以按顺序排序。
现在将 window
1 的所有对象收集到一个 List
中,将 window
2 的对象收集到另一个列表中,等等。
HashMap<Integer, ArrayList<Testdata>> map = new HashMap<Integer, ArrayList<Testdata>>();
// Add all the objects like this
while (...) { // While there are more objects
Testdata td = ... // Get next object
List<TestData> list = map.get(td.window);
if (list == null) {
list = new ArrayList<Testdata>();
map.put(td.window, list);
}
list.add(td.sequence);
}
使用 Collections.sort(List) 对所有列表进行排序:
for (ArrayList<TestData> list : map) {
Collections.sort(list);
}
然后每个窗口都有一个列表,可通过 map.get(window)
访问。这些列表中的每一个都将具有最低 sequence
的对象作为其第一个对象,第二低的作为第二个对象等。-> 窗口位置是对象的索引 + 1。
编辑:
如果您的对象已经按窗口和顺序排序(在一个列表中),您可以这样做来分配窗口位置:
int window = 1;
int wp = 0;
for (Testdata td : list) {
if (td.window > window) {
wp = 1;
window = td.window;
} else {
wp++;
}
td.windowposition = wp;
}
关于java - 对 Java 对象进行排序并根据属性查找相对位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11987011/
我们有一个应用程序,我们可以在其中设计标签,然后根据设计创建图像并将其发送到打印机。图像在通过 TCP/IP 发送之前被制成字节流。 简而言之,我们有一个旋转设置,可以自动计算标签上不同元素的位置。而
我们在 IIS7 中有一个 MVC3 应用程序:http://mydomain.com/myapplication/ javascript 中的相对 URL 是什么: http://mydomain.
我的例子:http://jsfiddle.net/kwnk8qup/ 我的代码: container(父 div
如何将元素放置在右侧,并保持后面元素的持久位置? 我想在没有 Efficiently ... 的情况下将 text-align: right 对齐到右侧,并保留 t2 和 t3 元素之间的空间。 当我
假设我有一个包含以下文件的模块包。一个空文件 C:\codes\package\__init__.py 和一些重要文件: 一个位于C:\codes\package\first.py def f():
我正在尝试使用以下代码在屏幕上相对移动光标: input.type = INPUT_MOUSE; ZeroMemory(&input, sizeof(input)); input.mi.mouseDa
我排列了 3 个 div;中心 div 有许多 position:absolute 图像(它们重叠——一次显示一个图像;其余的是 display:none;对于 jQuery 交叉淡入淡出,这不是密切
我在 SQL 2000 数据库中有以下简化的表结构: ID AppName Key Value EffectiveDate -- ------- ----- ------- ---
给定以下配置: server { listen 80; server_name site.com; location /proxy { proxy_pa
我正在使用这些方法动态加载图像和资源目录,但它们并不适用于所有情况 new Uri(@"pack://application:,,/Images/lession_tab.png"); 此方法不适用于图
在插入/更新许多行时,我知道SQLite的“问题”,但事实并非如此。 我正在更新包含约250条记录的表中的ONE一行(由PK索引)中的ONE字段。查询通常需要200毫秒左右的时间。听起来很少,但很大。
如何向左或向右滑动线性布局。在该线性布局中,默认情况下我有一个不可见的删除按钮,还有一些其他小部件,它们都是可见状态,当向左滑动线性布局时,我需要使其可见的删除按钮,当向右滑动时,我需要隐藏该删除按钮
Imagen 我有一个 2D 点 (x,y) 列表,它们在我的简单游戏中描述了 2D 地形。 然后我让 glVertex() 在 GL_POINTS 模式下绘制所有这些点。 然后我有一个球,它也有它的
我正在使用 Google CloudSQL 并具有以下简单代码: ;dbname=', 'root', '' ); $db->setAttribute ( PDO::ATTR_ERRMODE, PDO
我知道有几个类似的问题,但是,其中的示例并没有说明问题,或者我无法从中获利 - 我真可耻。 所以我的问题是在带有 GUI 的简单应用程序中加载图像。例如: 我在 "D:\javaeclipseprog
我对放置在表格单元格内的 div 有疑问。单元格具有固定的高度和 div 相对于 height:100% 定位。 jsfiddle example td { height:80px;
我正在抓取一些数据。 我需要的数据点之一是日期,但包含此数据的表格单元格仅包括月份和日期。幸运的是,年份被用作对表格进行分类的标题元素。 出于某种原因,year = table.find_elemen
我想要一个 DIV,最大 90% 高度或 90% 宽度。我只想将图片作为背景放入 DIV 中,并且完整的 svg-image 应该是可见的。在移动设备和桌面设备上。 CSS: .camera {
我是网页设计新手,想了解 CSS 中定位的概念。我对相对和绝对定位元素有一些了解。在下面的 fiddle 中,我有一个 作为 的父级.当我做 p position:absolute ,边框崩溃不像我
我在调整图像高度相对于浏览器窗口的大小时遇到了一些严重的困难——宽度可以很好地调整大小,但高度保持不变。我尝试过 height: 100% 和 height: auto,以及 height: 10
我是一名优秀的程序员,十分优秀!