条件循环在Java中

huangapple go评论126阅读模式
英文:

Condition for loops in java

问题

我有2个循环。

第一个:

  1. while (part[i].compareToIgnoreCase(part[i-1]) < 0) {
  2. smaller = myarr[i];
  3. myarr[i] = myarr[i-1];
  4. myarr[i-1] = smaller;
  5. i--;
  6. if (i <= 0) {
  7. break;
  8. }
  9. }

第二个:

  1. while (Integer.parseInt(part[i]) < Integer.parseInt(part[i-1])) {
  2. smaller = myarr[i];
  3. myarr[i] = myarr[i-1];
  4. myarr[i-1] = smaller;
  5. i--;
  6. if (i <= 0) {
  7. break;
  8. }
  9. }

如果您注意到,这两个循环执行相同的任务,只是条件不同。这些条件不能同时传递,只能一次使用一个条件(因为这些循环是方法的一部分,分别传入了两个内容不同的字符串数组)。

我可以将这两个循环合并在一起,并做些什么,以便一次只选择一个条件吗?

英文:

I have 2 loops.

The first:

  1. while(part[i].compareToIgnoreCase(part[i-1])&lt;0) {
  2. smaller = myarr[i];
  3. myarr[i] = myarr[i-1];
  4. myarr[i-1] = smaller;
  5. i--;
  6. if(i&lt;=0) {
  7. break;
  8. }

The second:

  1. while(Integer.parseInt(part[i]) &lt; Integer.parseInt(part[i-1])) {
  2. smaller = myarr[i];
  3. myarr[i] = myarr[i-1];
  4. myarr[i-1] = smaller;
  5. i--;
  6. if(i&lt;=0) {
  7. break;
  8. }

If you notice, both the loops perform the same task. Just the conditions are different. The conditions can not be passed in together i.e.only one can be used at a time.(Because these loops are a part of a method and 2 String arrays with different content are passed in.)

Can I somehow merge both of these together and do something so that only one condition is picked up at a time?

答案1

得分: 0

听起来你是在询问如何将一个Predicate(确切地说是BiPredicate<String, String>)作为参数传递。

  1. public void bubbleSort(String[] part, BiPredicate<String, String> predicate) {
  2. while(predicate.test(part[i], part[i-1])) {
  3. // 进行冒泡排序操作
  4. }
  5. }
  6. public void bubbleSort(String[] part) {
  7. if (isText) {
  8. bubbleSort(part, (l, r) -> l.compareToIgnoreCase(r) < 0);
  9. } else {
  10. bubbleSort(part, (l, r) -> Integer.parseInt(l) < Integer.parseInt(r));
  11. }
  12. }

尽管如此,有更好的方法来对数组进行排序! 条件循环在Java中

  1. Arrays.sort(part, String::compareToIgnoreCase); // 按文本排序
  2. Arrays.sort(part, Comparator.comparingInt(Integer::parseInt)); // 按数字排序
英文:

Sounds like you are asking how to take a Predicate (BiPredicate&lt;String, String&gt; to be precise) as a parameter.

  1. public void bubbleSort(String[] part, BiPredicate&lt;String, String&gt; predicate) {
  2. while(predicate.test(part[i], part[i-1])) {
  3. // Do bubblesort stuff
  4. }
  5. }
  6. public void bubbleSort(String[] part) {
  7. if (isText) {
  8. bubbleSort(part, (l, r) -&gt; l.compareToIgnoreCase(r) &lt; 0);
  9. } else {
  10. bubbleSort(part, (l, r) -&gt; Integer.parseInt(l) &lt; Integer.parseInt(r));
  11. }
  12. }

That said there are better ways to sort arrays! 条件循环在Java中

  1. Arrays.sort(part, String::compareToIgnoreCase); // Sort by text
  2. Arrays.sort(part, Comparator.comparingInt(Integer::parseInt)); // Sort by number

huangapple
  • 本文由 发表于 2020年7月28日 07:24:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/63124997.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定