如何在java中使用for循环移动arraylist中的元素
我需要帮助编码以下内容: 在NotebookTest驱动程序中,使用For循环将笔记向上移动指定的移动次数(将存储在 变量)。将此变量的值设置为2并测试For循环。使用您在步骤D中创建的方法 制作一个对应的For循环,将音符向下移动指定的次数 笔记本类代码:如何在java中使用for循环移动arraylist中的元素,java,for-loop,arraylist,Java,For Loop,Arraylist,我需要帮助编码以下内容: 在NotebookTest驱动程序中,使用For循环将笔记向上移动指定的移动次数(将存储在 变量)。将此变量的值设置为2并测试For循环。使用您在步骤D中创建的方法 制作一个对应的For循环,将音符向下移动指定的次数 笔记本类代码: public class Notebook { private ArrayList < String > notes; public Notebook() { this.notes = new
public class Notebook {
private ArrayList < String > notes;
public Notebook() {
this.notes = new ArrayList < String > ();
}
public ArrayList < String > getNotes() {
return notes;
}
//adds a note to list and won't allow duplicate
public void addNote(String note) {
if (!notes.contains(note)) {
notes.add(note);
}
}
//this was the original addNote Method
/*public void addNote(String note) {
this.notes.add(note);
}*/
//removes a note
public void deleteNote(String note) {
this.notes.remove(note);
}
//returns the size of list
public int numberOfNotes() {
return notes.size();
}
// gets the position of a note based on its value
public int getNoteNumber(String note) {
return notes.indexOf(note);
}
//returns text in a position
public String getNote(int note) {
return notes.get(note);
}
//updates text of a given position
public void setNote(int note, String nt) {
notes.set(note, nt);
}
//moves note up one in list
public void moveNoteUp(String note) {
Collections.swap(notes, notes.indexOf(note), notes.indexOf(note) - 1);
}
//moves note down one in list
public void moveNoteDown(String note) {
Collections.swap(notes, notes.indexOf(note), notes.indexOf(note) + 1);
}
}
所以你需要写代码来做这个或者测试这个?如果是这样,请尝试:
public void moveNoteUpBy(String note, int x){
for(int n=0; n<x; n++){
moveNoteUp(...);
}
}
public void moveNoteUpBy(字符串注释,int x){
对于(int n=0;n请看一下:
public void moveNoteUpBy(String note, int x){
for(int n=0; n<x; n++){
moveNoteUp(...);
}
}