for(int i=0;i<b;i++) { list1.add(0,list1.remove(a-1)); }
时间: 2023-12-30 16:05:45 浏览: 30
这段代码的作用是将列表list1的最后a个元素移动到列表的开头,并且重复这个操作b次。具体来说,代码做了以下几个步骤:
1. 循环b次,每次循环都将列表list1的最后a个元素取出来,然后添加到列表list1的开头。
2. 列表的remove(int index)方法可以取出指定位置的元素并将其从列表中删除,因此list1.remove(a-1)可以取出列表list1中的最后一个元素。
3. 列表的add(int index, E element)方法可以将元素插入到列表的指定位置,因此list1.add(0, list1.remove(a-1))可以将列表list1的最后一个元素插入到列表的开头。
通过这个操作,列表list1的最后a个元素会被移到列表的开头,而其他元素的位置不变。重复这个操作b次后,列表list1的前a*b个元素会按照原来的顺序向后移动a个位置,而列表list1的后面元素的位置不变。
相关问题
public class Game { private static final String[] suits = {"♥","♣","♦","♠"}; public List buyPoker() { List<Poker> pokers = new ArrayList<>(); for (int i = 0; i < 4; i++) { for (int j = 0; j <= 13; j++) { Poker poker = new Poker(suits[i],j); pokers.add(poker); } } return pokers; } public void shuffle(List<Poker> pokers) { for (int i = pokers.size()-1; i > 0; i--) { Random random = new Random(); int index = random.nextInt(i); swap(pokers,i,index); } } private void swap(List<Poker> pokers,int i,int j) { Poker tmp = pokers.get(i); pokers.set(i,pokers.get(j)); pokers.set(j,tmp); } public List<List<Poker>> game(List<Poker> pokers) { List<List<Poker>> hand = new ArrayList<>(); List<Poker> hand1 = new ArrayList<>(); List<Poker> hand2 = new ArrayList<>(); List<Poker> hand3 = new ArrayList<>(); for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { Poker removePoker = pokers.remove(0); hand.get(j).add(removePoker); } } return hand; } }有错吗
在game方法中,你创建了一个空的List<List<Poker>> hand,但没有将任何元素添加到它中。因此,在尝试将元素添加到hand.get(j)时,会抛出NullPointerException异常。为了解决这个问题,你需要在game方法中为hand添加3个空列表,然后才能向它们中添加元素。
你需要将game方法中的代码修改如下:
```
public List<List<Poker>> game(List<Poker> pokers) {
List<List<Poker>> hand = new ArrayList<>();
List<Poker> hand1 = new ArrayList<>();
List<Poker> hand2 = new ArrayList<>();
List<Poker> hand3 = new ArrayList<>();
hand.add(hand1);
hand.add(hand2);
hand.add(hand3);
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
Poker removePoker = pokers.remove(0);
hand.get(j).add(removePoker);
}
}
return hand;
}
```
这样就能成功地将元素添加到hand中了。
public class JosephusProblem { public static void main(String[] args) { int n = 7; // 总人数 int m = 3; // 数到 m 出列 ArrayList<Integer> list = new ArrayList<Integer>(); for (int i = 1; i <= n; i++) { list.add(i); } int k = 0; // 开始报数的位置 while (list.size() > 1) { k = (k + m - 1) % list.size(); System.out.println("出列:" + list.remove(k)); } System.out.println("最后留下来的是:" + list.get(0)); } }转换为伪代码
好的,以下是该Java程序的伪代码:
```
// 伪代码示例:约瑟夫问题
// 设定总人数和数到 m 出列的规则
let n = 7; // 总人数
let m = 3; // 数到 m 出列
// 初始化人数列表
let list = [];
for (let i = 1; i <= n; i++) {
list.push(i);
}
// 开始循环报数,直到只剩下一个人
let k = 0; // 开始报数的位置
while (list.length > 1) {
k = (k + m - 1) % list.length;
print("出列:" + list[k]);
list.splice(k, 1); // 移除出列的人
}
// 输出最后留下来的人
print("最后留下来的是:" + list[0]);
```
在这个伪代码中,我们使用了类似于Java的语法,但是去掉了Java的具体语法和细节,使用了类似于伪代码的语法和标记来描述算法和程序的逻辑流程。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![ppt](https://img-home.csdnimg.cn/images/20210720083527.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)