简体   繁体   English

排序顺序和查找重复模式

[英]Sorting Order and Finding repeating patterns

      //other arrays sequences sample
      {23,45,38, 9,43,25,18}
      {21,33,22, 5, 1,44,16}
      {28,24, 5,42,15,49,41}
      {43,18,50,29,22,32,25}
      {33,38,27,35,25, 1,12}
      {21,33,22, 5, 1,44,16}
      {28,24, 5,42,15,49,41}

I have a little project where i have a list of sequences and i would like to have them rearranged from small to large and reprinted for use to find repeating sequences and show how many time each sequence repeats 我有一个小项目,其中有一个序列列表,我希望将它们从小到大重新排列,然后重新打印以查找重复序列并显示每个序列重复多少次

Now I wrote a small code sorting the sequences from small to large with the aid of a friend in order to solve the first problem. 现在,我编写了一个小代码,在一位朋友的帮助下,将序列从大到小进行了排序,以解决第一个问题。

But I would like it to work with multiple sets and arrange each set and print them on there own line, In order for me to find the repeating patterns 但是我希望它可以处理多组并排列每组并将其打印在一行上,以便我找到重复的图案

Yet every time i change the {int} to accommodate more sequences the code fails 但是每次我更改{int}以容纳更多序列时,代码都会失败

public class Main {
    public static void main(String args[]) {
        int x[] = {37, 36, 20, 23, 44, 27, 24};

        for (int y = 0; y <= x.length; y++) {
            for (int z = 0; z <= x.length - 2; z++) {
                if (x[z] > x[z + 1]) {

                    int temp = 0;
                    temp = x[z];

                    x[z] = x[z + 1];
                    x[z + 1] = temp;
                }
            }
        }

        for (int y = 0; y < x.length; y++) {
            System.out.print(x[y]);
        }
    }
}

as part of the second problem finding the repeating sequence i'm looking into dis post and busy reworking to see if it will work. 作为查找重复序列的第二个问题的一部分,我正在研究发布并忙于重做以查看其是否有效。

Yet as far as i see it will find the repeating number and not sequence witch is where i'm stuck. 但是据我所知,它将找到重复的数字,而不是序列,而女巫就是我所困的地方。

How to find repeating sequence of Integers in an array of Integers? 如何在整数数组中查找整数的重复序列?

I'm still new to java and any aid will be appreciated Kind Regards Deon 我还是Java的新手,对您的帮助将不胜感激Deon

I suggest you split this in two separate questions. 我建议您将其分为两个独立的问题。 I will now try to answer to the sorting problem, however, I'm not quite sure if I understand your problem here. 我现在将尝试回答排序问题,但是,我不确定在这里是否理解您的问题。

You implemented the bubble sort . 您实现了冒泡排序 You can see in the linked Wikipedia article how this is correctly implemented. 您可以在链接的Wikipedia文章中看到如何正确实现。 Basically there is a do-while loop as the outer loop. 基本上,有一个do-while循环作为外部循环。

Alternatively, you could also use Java's Arrays#sort 另外,您也可以使用Java的Arrays#sort

EDIT 编辑

You can create a two dimensional array for your sequences like this: 您可以为序列创建一个二维数组,如下所示:

  int[][] seq = {
      {23,45,38, 9,43,25,18},
      {21,33,22, 5, 1,44,16},
      {28,24, 5,42,15,49,41},
      {43,18,50,29,22,32,25},
      {33,38,27,35,25, 1,12},
      {21,33,22, 5, 1,44,16},
      {28,24, 5,42,15,49,41}
  };

Then, you can iterate over seq and sort each contained array individually: 然后,您可以遍历seq并对每个包含的数组进行单独排序:

for(int i = 0; i < seq.length; i++) {
  int[] x = seq[i];
  // sort x
}

您不能只使用Arrays.sort(x)对数组进行排序。

Thanks Hage, It's small but I learned a lot Here is the working code 感谢Hage,虽然很小,但我学到了很多这是工作代码

    public class Main {

        public static void main(String[] args) {
            int[][] seq={{ 42, 22, 40, 1, 11, 5, 43 },
                         { 13, 11, 18, 45, 3, 44, 19 },
                         { 46, 1, 32, 47, 35, 7, 36 },
                         { 48, 21, 38, 29, 3, 12, 11 },
            };

            for (int i=0; i < seq.length; i++) {
                int[] x=seq[i];

                Arrays.sort(x);
                for (int number : x) {
                        System.out.print(number  + " ");}
                        System.out.print("\n");
                    }
                }
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM