当前位置: 首页 > 知识库问答 >
问题:

在java中,如何对一个数组进行排序,从而对另一个数组进行排序?

景元忠
2023-03-14

因此,我试图学习如何为类项目排序数组。我想知道如何对一个数组进行排序,从而对另一个数组进行排序。在下面的代码中,我可以对年份数组进行排序,但我如何才能使更改这一数组将名称和艺术家数组都更改为它们排列的数组呢?此外,如果你有任何建议,让代码对眼睛不那么苛刻,请告诉我,我正在努力掌握这个概念。

public class Main
  {
    public static int q;
    public static int t;
    public static int u;
    public static int v;
    public static int w = -1;
    public static int x = 0;
    public static int y = 0;
    public static int z = 0;
    public static String[] names;
    public static int[] years;
    public static String[] artists;
    public static String temp[];
    public static int tempO[];
    public static String[] PostMalone;
    public static String[] MichaelJackson;
    public static String[] ElvisPresley;
    public static class Music {
    private int year;
    private String title;
    private String artist;
    private int placement;
    

    // Constructor for objects of class Music
    Music(String t, int y, String a) 
    {
        // initialize instance variables
        this.title = t;
        this.year = y;
        this.artist = a;
      setPlacement();
    }
    public void setPlacement() {
      w+=1;
      this.placement = w;
    }
      public int getPlacement() {
        return placement;
      }

    public String getTitle()
    {
        return title;
    }
   
    public void setTitle(String t)
    {
        title = t;
    }
   
    public String getArtist()
    {
        return artist;
    }
    
    public void setArtist(String a)
    {
        artist = a;
    }
   
    public int getYear()
    {
        return year;
    }
    
    public void setTitle(int y)
    {
        year = y;
    }
    }
      public static void arrNames(Music name) {
      temp = new String[x];
      int g = x;
      int h = 0;
      while (g>0) {
        temp[h] = names[h];
        g-=1;
        h+=1;
      }
      x+=1;
      names = new String[x];
      g = x;
      h = 0;
      while (g>1) {
        names[h] = temp[h];
        g-=1;
        h+=1;
      }
      names[(x-1)] = name.getTitle();
    }
    public static void arrYear(Music name) {
      tempO = new int[y];
      int g = y;
      int h = 0;
      while (g>0) {
        tempO[h] = years[h];
        g-=1;
        h+=1;
      }
      y+=1;
      years = new int[y];
      g = y;
      h = 0;
      while (g>1) {
        years[h] = tempO[h];
        g-=1;
        h+=1;
      }
      years[(y-1)] = name.getYear();
    }
      public static void arrArtist(Music name) {
      temp = new String[z];
      int g = z;
      int h = 0;
      while (g>0) {
        temp[h] = artists[h];
        g-=1;
        h+=1;
      }
      z+=1;
      artists = new String[z];
      g = z;
      h = 0;
      while (g>1) {
        artists[h] = temp[h];
        g-=1;
        h+=1;
      }
      artists[z-1] = name.getArtist();
    }
  public static void setGroup(Music name) {
      arrNames(name);
      arrYear(name);
      arrArtist(name);
    }
      public static void getGroups() {
      int a = x;
      int b = y;
      int c = z;
      int d = 0;
      int e = 0;
      int f = 0;
      System.out.println("Names Category:  ");
      while (a > 0) {
        System.out.print(names[d] + "  ");
        d+=1;
        a-=1;
        }
      System.out.println("");
      System.out.println("Years Category:  ");
      while (b > 0) {
        System.out.print(years[e] + "  ");
        e+=1;
        b-=1;
      }
      System.out.println("");
      System.out.println("Artists Category:  ");
      while (c > 0) {
        System.out.print(artists[f] + "  ");
        f+=1;
        c-=1;
      }
      System.out.println("");
      }
      public static void sortByYear(int arr[], int n)
{
    int i, l, j;
    for (i = 1; i < n; i++)
    {
        l = arr[i];
        j = i - 1;
        while (j >= 0 && arr[j] > l)
        {
            arr[j + 1] = arr[j];
            j = j - 1;
        }
        arr[j + 1] = l;
        
    }
}
    
       public static void printArray(int[] array)
    {
      System.out.print("Here are all of the release dates in order: ");
        for (int l = 0; l < array.length; l++) {
            System.out.print(array[l] + " ");
        }
        System.out.println();
    }
    
    
  
  public static void main(String[] args) {
    Music CH = new Music("Can't Help Falling In Love", 1961, "Elvis Presley");
    setGroup(CH);
    Music SF = new Music("Sunflower", 2018, "Post Malone");
    setGroup(SF);
    Music JR = new Music("Jailhouse Rock", 1957, "Elvis Presley");
    setGroup(JR);
    Music BI = new Music("Beat It", 1982, "Michael Jackson");
    setGroup(BI);
    Music TH = new Music("Thriller", 1982, "Michael Jackson");
    setGroup(TH);
    Music CG = new Music("Congratulations", 2016, "Post Malone");
    setGroup(CG);
    Music BL = new Music("Burning Love", 1972, "Elvis Presley");
    setGroup(BL);
    Music SC = new Music("Smooth Criminal", 2001, "Michael Jackson");
    setGroup(SC);
    Music BN = new Music("Better Now", 2018, "Post Malone");
    setGroup(BN);
    Music RS = new Music("Rockstar", 2018, "Post Malone");
    setGroup(RS);
    getGroups();
    sortByYear(years, x);
    printArray(years);
    getGroups(); //error here, I want the names category and artists category to line up with the years category. How would I do that?
    }
}

共有1个答案

范翰飞
2023-03-14

我将进一步阐述我的观点:

假设您有以下阵列:

int[] years = ...;
String[] titles = ...;
String[] artists = ...;

尝试创建一个歌曲数组,而不是使用它们,例如:

//simplified class, use proper access modifiers, getters and setters
class Song {
  int year;
  String title;
  String artist;
}

Song[] songs = ...;

然后使用比较器对其进行排序

Arrays.sort(songs, new Comparator<Song>() {
  public int compare(Song left, Song right) {
    return Integer.compare(left.year, right.year);
  }
});

这故意没有使用lambdas,以使初学者更容易理解。以下是lambdas的外观:

 Arrays.sort(songs, Comparator.comparing(song -> song.year));

 类似资料:
  • 问题内容: 假设我有一个自定义类的数组,每个类都包含一个名为 我还有一个任意值数组,称为,如下所示: 我的目标是对所有QB 进行排序,然后再对所有WR,RB和TE 进行排序。 我当前的操作方式是遍历中的每个元素,然后遍历所有播放器以附加到新数组。但是,我想不出一种更简单(更有效)的方法来做到这一点。非常感谢任何提示或指示。谢谢。 问题答案: 编辑: 我原来的方法是狗屎。这篇文章吸引了很多人的注意力

  • 问题内容: 是否可以对看起来像这样的数组进行排序和重新排列: 匹配此数组的安排: 不幸的是,我没有任何要跟踪的ID。我将需要优先处理items-array,以使其尽可能接近sortingArr。 更新: 这是我正在寻找的输出: 任何想法如何做到这一点? 问题答案: 就像是: 这是一个较短的代码,但是会破坏数组:

  • 我有两个整数数组,我试图根据另一个数组对第一个数组进行排序。 例如。和 b = {1,2,2,0,0, 在 B 中排序的值是 A 中每个整数的实值 排序后我期望的预期结果是: 这是我用的代码 它给出了我的输出:<code>a={2,3,1,0,0,6}和

  • 问题内容: 我有多个数组,我想根据其中一个的排序顺序对所有数组进行排序,如下所示: 我希望函数执行后,数组将如下所示: 问题答案: 您可以执行以下操作:首先根据键控数组的索引的索引对它们进行索引的值对它们进行排序,然后使用: 如果要在任何类型的集合上使它通用(但仍以与std lib集合算法相同的样式返回数组): 以及带有自定义比较器的版本:

  • 我有多个数组,我想根据其中一个数组的排序顺序对所有数组进行排序,如下所示: 我预计函数执行后的数组将如下所示:

  • 问题内容: 从这个问题的答案(使用一个较少的内存,沿着一个特定轴对另一个numpy数组进行排序)中,我学习了如何根据另一个numpy数组的值对多维numpy数组进行排序,而又不创建太多额外的数组。 但是,仅当数组和具有相同形状时才有效。我的数组是一维数组,但数组是ND数组(未指定N)。通过一维数组的值在特定轴上对数组进行排序是一种好方法(高效)吗? 问题答案: 使用与关键字参数: 如果要使用花式索