Java:将不同类型的数组相互排序
我需要根据另一个数组中保存的位置对数组进行排序。
我所拥有的有效,但有点慢,有没有更快/更好的方法来实现这个?
2 部分:
第 1 部分
int i = mArrayName.size();
int temp = 0;
for(int j=0;j<i;j++){
temp = mArrayPosition.get(j);
mArrayName.set(temp, mArrayNameOriginal.get(j));
}
在这部分中,mArrayPosition 是我希望 mArrayName 所处的位置。
例如。
输入:
mArrayName=(一、二、三)
mArrayPosition = (2,0,1)
输出:
mArrayName= (三,一二)
第二部分
int k=0;
int j=0;
do{
if(mArrayName.get(k)!=mArrayNameOriginal.get(j)){
j++;
}else{
mArrayIdNewOrder.set(k, mArrayId.get(j));
k++;
j=0;
}
}while(k < mArrayName.size());
}
在这一部分中,mArrayName 是重新排序后的名称数组,mArrayNameOriginal 是原始名称数组。
例如
mArrayName = (三,一,二)
mArrayNameOriginal = (一、二、三)
现在我想比较这两个数组,找到哪些条目相等,并将其与一个包含 rowId 编号的新数组相关联。
例如
输入:
mArrayId = (001,002,003)
输出:
mArrayIdNewOrder = (003,001,002)
那么我将让 mArrayIdNewOrder id 与 mArrayName 中的正确名称相匹配。
就像我说的,这些方法有效,但是有没有更快/更好的方法呢?我尝试查看 Arrays.sort 和比较器,但它们似乎只按字母或数字排序。我看到类似我可以在比较器中创建自己的规则,但它最终可能会与我已经拥有的类似。
很抱歉提出了令人困惑的问题。如果需要的话,我会尽力消除任何歧义。
I need to sort an array based on the positions held in another array.
What I have works, but it is kinda slow, is there a faster/better way to implement this?
2 Parts:
Part1
int i = mArrayName.size();
int temp = 0;
for(int j=0;j<i;j++){
temp = mArrayPosition.get(j);
mArrayName.set(temp, mArrayNameOriginal.get(j));
}
In this part, mArrayPosition is the position I would like the mArrayName to be in.
Ex.
input:
mArrayName= (one, two, three)
mArrayPosition = (2,0,1)
output:
mArrayName= (three, one two)
Part 2
int k=0;
int j=0;
do{
if(mArrayName.get(k)!=mArrayNameOriginal.get(j)){
j++;
}else{
mArrayIdNewOrder.set(k, mArrayId.get(j));
k++;
j=0;
}
}while(k < mArrayName.size());
}
In this part, mArrayName is the reordered name array, mArrayNameOriginal is the original name array.
Ex.
mArrayName = (three, one, two)
mArrayNameOriginal = (one, two, three)
Now I want to compare these two arrays, find which entries are equal and relate that to a new array that has their rowId number in it.
Ex.
input:
mArrayId = (001,002,003)
output:
mArrayIdNewOrder = (003,001,002)
So then I will have mArrayIdNewOrder id's matching up with the correct names in mArrayName.
Like I said these methods work, but is there a faster/better way to do it? I tried looking at Arrays.sort and comparators but they only seem to sort alphabetically or numerically. I saw something like I can create my own rules inside the comparator but it would probably end up being similar to what I already have.
Sorry for the confusing question. I'll try to clear up any ambiguities if needed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我发现的最佳性能读物是Android 的性能设计文档。您违反了一些对您有帮助的“Android 方式”做事方式。
您在每个循环内使用多个内部 getter 来获取看起来像简单的值。通过直接访问字段重做此操作。
为了获得额外的积分,请发布您的性能比较结果!我很想见他们!
The best performance read I've found is Android's Designing For Performance doc. You are violating a couple of the "Android way" style of doing things that will help you.
You are using multiple internal getters inside each loop for what looks like a simple value. Redo this by accessing the fields directly.
For extra credit, post your performance comparison results! I'd love to see em!
您可以使用某种形式的元组、某种类来保存 id 和 name。您只需要有一个 java.util.Comparator 来进行相应的比较,两个元素就会一起移动,您的代码就会更干净。
这个数据结构可能对程序的其余部分很方便......如果不是,只需再次将其删除即可完成。
You could use some form of tuple, some class to hold both id and name. You'll just to have a java.util.Comparator that compares it accordingly, both elements will move together and your code will be cleaner.
This data structure might be convenient for the rest of your program... if not, just take things off it again and you're done.
如果您的订单索引很紧凑,即从索引 0 到 size - 1,那么只需使用数组并随后创建更新的列表?关于类似的事情
If your order indexes are compact, i.e. from index 0 to size - 1, then just use an array and create the updated list afterwards? About something like