简体   繁体   English

创建具有预定长度的数组ArrayList

[英]Creating an ArrayList of Arrays with predetermined length

Essentially, I'm looking to create an ArrayList of Arrays of Notes: 本质上,我正在寻找创建一个Notes数组的ArrayList:

   /**
   * Represents the entire musical score, with each index of the ArrayList representing each beat.
   * Each array of notes represent the notes that can play on a single beat (up to 120 notes, 12
   * notes over 10 octaves.
   */
  private List<Note[]> score;
  ...
  score = new ArrayList<>(8); // Initialize ArrayList with capacity of 8

The first (minor) problem I'm running into here is first that the ArrayList could end up being any size and may have to grow many times (which doubles the size each time it requires additional capacity), and the second (more important) problem is that I'm unable to set the length of the Arrays contained within the ArrayList to 120 as follows: 我遇到的第一个(次要)问题首先是ArrayList可能最终变成任何大小,并且可能需要增长很多次(每次需要额外容量时它的大小增加一倍),第二个(更重要)问题是我无法将ArrayList中包含的数组的长度设置为120,如下所示:

private List<Note[120]> score;

This brings me to the question of how I can initialize all of the Arrays that may or may not exist in the ArrayList (because we don't know when/if/how often the ArrayList size may change) to a size of 120, and, if that is not possible, whether or not there is potentially a better data structure to be used in this situation. 这让我想到如何初始化ArrayList中可能存在或不存在的所有数组(因为我们不知道ArrayList大小何时/多久可能更改)到120的大小,以及如果不可能,那么在这种情况下是否有可能使用更好的数据结构。 Thanks! 谢谢!

There is no data type for an array of length 120 , so you cannot have a List<Note[120]> or a List<Note[]> where every array is guaranteed to have length 120 . 长度为120的数组没有数据类型,因此您不能使用List<Note[120]>List<Note[]> ,其中每个数组都保证长度为120 What you can do is make your own class: 你能做的就是自己上课:

public final class TenOctaves {

    private static final int NUMBER_OF_NOTES = 120;

    private final Note[] notes = new Note[NUMBER_OF_NOTES];

    public void setNote(int position, Note note) {
         if (position < 0 || position >= NUMBER_OF_NOTES)
             throw new IllegalArgumentException();
         notes[position] = note;
    }

    public Note getNote(int position) {
        if (position < 0 || position >= NUMBER_OF_NOTES)
             throw new IllegalArgumentException();
        return notes[position];
    }
}

Then you could have a List<TenOctaves> instead. 然后你可以改为使用List<TenOctaves>

If I'm understanding you correctly, you could do with subclassing ArrayList and implementing your own special add logic. 如果我正确理解你,你可以用子类化ArrayList并实现你自己的特殊add逻辑。

public class NoteList extends ArrayList<Note[]> {

    private final int SCORE_LENGTH = 120;
    private final int MAX_ELEMENTS = 8;

    @Override
    public boolean add(Note[] score) {
        return this.size() <= MAX_ELEMENTS && score.length == SCORE_LENGTH && super.add(score);
    }
}

You won't be able to add elements to the list if: 如果符合以下条件,您将无法在列表中添加元素:

  • the size of the list is larger than 8 列表的大小大于8
  • the length of the array you're going to add is not 120 你要添加的数组的长度不是120

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

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