As you know the first 6 letters of the English alphabet is a, b, c, d, e, f
. I want to swap(i.e. switch) the 2nd letter which is b and the fifth letter which is e. In other words, my goal is that after the code is executed my compiler will print the following: a, e, c, d, b,f
.
如您所知,英文字母的前6个字母是a,b,c,d,e,f。我想交换(即切换)第二个字母,即b和第五个字母,即e。换句话说,我的目标是在执行代码后,我的编译器将打印以下内容:a,e,c,d,b,f。
When I google about swapping things in an array list I keep finding examples that contain collections.swap.. Well in class we did not learn about this. I am asking that you don't include that in your answer. The following 2 lines of codes are very simple examples of the kind of thing that is supposed to be in the code that we submit. The first example is public static void swap(int i, int j, ArrayList<String> myList).
The second example is void swap(int i, int j, ArrayList myList)
当我谷歌关于在数组列表中交换东西时,我一直在寻找包含collections.swap的示例..在课堂上我们没有了解这一点。我要求你不要在答案中加入。以下两行代码是应该在我们提交的代码中的那种事物的非常简单的例子。第一个例子是public static void swap(int i,int j,ArrayList
The professor didn't exactly want to us to write this code from scratch. She wanted us to fix the code she gave us on the hand out the told us what we needed to do for this assignment. This is the incorrect code:
教授并不想让我们从头开始编写这段代码。她希望我们修改她给我们的代码,告诉我们我们需要为这项任务做些什么。这是错误的代码:
import java.util.ArrayList;
public class abc{
public static void main(String[]args){
ArrayList<String> myList= new ArrayList();
myList.add("a");
myList.add("b");
myList.add("c");
myList.add("d");
myList.add("e");
myList.add("f");
swap(1,4,myList);
System.out.println(myList);
}
public static void swap(int i, int j, ArrayList<String> myList) {
String temp = myList.get(i);
myList.add(i, myList.get(j));
myList.add(j, temp);
}
(I tried to give that a gray background like how code is supposed to look but, it didn't work out that way.)
(我试图给出一个灰色的背景,就像代码应该看起来一样,但是这样做并不成功。)
This code prints [a, e, b, c, b, d, e, f]
but the first paragraph that I wrote explains what I'm trying to print. I spent such a long time trying to get this to work and I can't. Please help me get Jgrasp to do what I want to it to do (refer to first paragraph.) If possible please make your code look as close to the incorrect code as possible but of course I would like it to be correct.
这段代码打印[a,e,b,c,b,d,e,f],但我写的第一段解释了我要打印的内容。我花了这么长时间试图让它工作,我不能。请帮助我让Jgrasp做我想做的事情(请参阅第一段。)如果可能,请尽可能使代码看起来尽可能接近错误的代码,但我当然希望它是正确的。
4 个解决方案
#1
2
use List.set instead of add in your swap method, see: Java ArrayList replace at specific index
使用List.set而不是在swap方法中添加,请参阅:Java ArrayList替换特定索引
public static void swap(int i, int j, ArrayList<String> myList) {
String temp = myList.get(i);
myList.set(i, myList.get(j));
myList.set(j, temp);
}
#2
1
Use the built-in Collections.swap
使用内置的Collections.swap
Replace
String temp = myList.get(i);
myList.add(i, myList.get(j));
myList.add(j, temp);
with
Collections.swap(myList, i , j);
#3
0
add(E e)
Appends the specified element to the end of this list (optional operation).
将指定的元素追加到此列表的末尾(可选操作)。
add(int index, E element)
add(int index,E element)
Inserts the specified element at the specified position in this list (optional operation).
将指定元素插入此列表中的指定位置(可选操作)。
set(int index, E element)
set(int index,E element)
Replaces the element at the specified position in this list with the specified element (optional operation).
用指定的元素替换此列表中指定位置的元素(可选操作)。
import java.util.ArrayList;
public class abc{
public static void main(String[]args){
ArrayList<String> myList= new ArrayList();
myList.add("a");
myList.add("b");
myList.add("c");
myList.add("d");
myList.add("e");
myList.add("f");
swap(1,4,myList);
System.out.println(myList);
}
public static void swap(int i, int j, ArrayList<String> myList) {
String temp = myList.get(i);
myList.set(i, myList.get(j));
myList.set(j, temp);
}
}
#4
0
If you want to swap using positions, you can use the following:
如果要使用职位进行交换,可以使用以下内容:
import java.util.Collections;
ArrayList<String> list = new ArrayList<String>();
list.add("a");
list.add("b");
list.add("c");
list.add("d");
list.add("e");
// swapping
Collections.swap(list, 1, 3);
This will swap 1st element (b) with 3rd (d) to give [ a, d, c, b, e ]
这会将第一个元素(b)与第三个(d)交换为[a,d,c,b,e]
#1
2
use List.set instead of add in your swap method, see: Java ArrayList replace at specific index
使用List.set而不是在swap方法中添加,请参阅:Java ArrayList替换特定索引
public static void swap(int i, int j, ArrayList<String> myList) {
String temp = myList.get(i);
myList.set(i, myList.get(j));
myList.set(j, temp);
}
#2
1
Use the built-in Collections.swap
使用内置的Collections.swap
Replace
String temp = myList.get(i);
myList.add(i, myList.get(j));
myList.add(j, temp);
with
Collections.swap(myList, i , j);
#3
0
add(E e)
Appends the specified element to the end of this list (optional operation).
将指定的元素追加到此列表的末尾(可选操作)。
add(int index, E element)
add(int index,E element)
Inserts the specified element at the specified position in this list (optional operation).
将指定元素插入此列表中的指定位置(可选操作)。
set(int index, E element)
set(int index,E element)
Replaces the element at the specified position in this list with the specified element (optional operation).
用指定的元素替换此列表中指定位置的元素(可选操作)。
import java.util.ArrayList;
public class abc{
public static void main(String[]args){
ArrayList<String> myList= new ArrayList();
myList.add("a");
myList.add("b");
myList.add("c");
myList.add("d");
myList.add("e");
myList.add("f");
swap(1,4,myList);
System.out.println(myList);
}
public static void swap(int i, int j, ArrayList<String> myList) {
String temp = myList.get(i);
myList.set(i, myList.get(j));
myList.set(j, temp);
}
}
#4
0
If you want to swap using positions, you can use the following:
如果要使用职位进行交换,可以使用以下内容:
import java.util.Collections;
ArrayList<String> list = new ArrayList<String>();
list.add("a");
list.add("b");
list.add("c");
list.add("d");
list.add("e");
// swapping
Collections.swap(list, 1, 3);
This will swap 1st element (b) with 3rd (d) to give [ a, d, c, b, e ]
这会将第一个元素(b)与第三个(d)交换为[a,d,c,b,e]