public boolean checkLeft() { // 1. 创建新数组, 用于备份原数组数据. int[][] newArr = new int[4][4]; // 2. 将原数组数据, 拷贝到新数组中. copyArray(datas, newArr); // 3. 调用左移动方法, 对原数组数据进行左移动 moveToLeft(); // 4. 使用移动后的数据, 和备份的数据逐个进行比对, 并使用flag变量进行记录. // 设计思路: // true : 可以移动 // false : 不可以移动. boolean flag = false; // datas : 2 // newArr : 0 lo: for (int i = 0; i < datas.length; i++) { for (int j = 0; j < datas[i].length; j++) { if (datas[i][j] != newArr[i][j]) { // 主要元素有一个是不相同的, 就代表数据可以变化, 数据可以移动. flag = true; break lo; } } } // 5. 确定信息后, 恢复原数组数据(再做一次拷贝) copyArray(newArr, datas); // 6. 返回结果信息. return flag; }
时间: 2024-02-10 10:12:40 浏览: 27
这是一个用于判断游戏中是否可以向左移动的函数。具体实现步骤如下:
1. 创建一个新的二维数组,用于备份原始数据。
2. 将原始数据拷贝到新数组中。
3. 调用左移方法,对原始数组进行左移操作。
4. 使用移动后的数据和备份的数据逐个进行比对,并使用flag变量进行记录。如果有至少一个主要元素左移后的值与原始数组不同,就代表数据可以变化,可以移动。如果所有主要元素左移后的值与原始数组相同,就代表数据不能移动。
5. 确定信息后,将新数组的数据再次拷贝到原始数组中,恢复原始数据。
6. 根据flag变量的值返回结果信息,true表示可以向左移动,false表示不能向左移动。
相关问题
为什么以下代码的学生测试类显示灰色,请找出这段代码不能运行的原因,并修改package MianXiangDuiXiangDemo3; public class StudentsTest { public static boolean main(String[] args) { //创建数组 Students[] arr = new Students[3]; //创建学生对象 Students stu1 = new Students("财", 1, 24); Students stu2 = new Students("来", 2, 26); Students stu3 = new Students("旺", 3, 21); //添加到数组 arr[0] = stu1; arr[1] = stu2; arr[2] = stu3; //添加一个学生对象 Students stu4 = new Students("盛", 4, 23); //唯一性判断,已存在,则不添加,不存在,则加 boolean flag = contains(arr, stu4.getId()); if (flag) { //存在 System.out.println("id存在,请重新添加"); } else { //不存在 //数组存满,则创建一个新的数组,新的数组长度 = +1 //数组没满,直接添加 int count = getcount(arr); if (count == arr.length) { //数组存满 //创建一个新的数组,新的数组长度 = +1 Students[] newArr = creatNewArr(arr); newArr[count] = stu4; printArr(newArr); } else { //数组没满 arr[count] = stu4; printArr(arr); } } return flag; }
以下是修改后的代码:
```java
package MianXiangDuiXiangDemo3;
public class StudentsTest {
public static void main(String[] args) {
//创建数组
Students[] arr = new Students[3];
//创建学生对象
Students stu1 = new Students("财", 1, 24);
Students stu2 = new Students("来", 2, 26);
Students stu3 = new Students("旺", 3, 21);
//添加到数组
arr[0] = stu1;
arr[1] = stu2;
arr[2] = stu3;
//添加一个学生对象
Students stu4 = new Students("盛", 4, 23);
//唯一性判断,已存在,则不添加,不存在,则加
boolean flag = contains(arr, stu4.getId());
if (flag) { //存在
System.out.println("id存在,请重新添加");
} else { //不存在
//数组存满,则创建一个新的数组,新的数组长度 = +1
//数组没满,直接添加
int count = getCount(arr);
if (count == arr.length) { //数组存满
//创建一个新的数组,新的数组长度 = +1
Students[] newArr = createNewArr(arr);
newArr[count] = stu4;
printArr(newArr);
} else { //数组没满
arr[count] = stu4;
printArr(arr);
}
}
}
public static boolean contains(Students[] arr, int id) {
for (Students s : arr) {
if (s != null && s.getId() == id) {
return true;
}
}
return false;
}
public static int getCount(Students[] arr) {
int count = 0;
for (Students s : arr) {
if (s != null) {
count++;
}
}
return count;
}
public static Students[] createNewArr(Students[] arr) {
Students[] newArr = new Students[arr.length + 1];
for (int i = 0; i < arr.length; i++) {
newArr[i] = arr[i];
}
return newArr;
}
public static void printArr(Students[] arr) {
for (Students s : arr) {
if (s != null) {
System.out.println(s);
}
}
}
}
```
原因是原代码中的 `main()` 方法返回类型为 `boolean`,而不是 `void`,因此无法运行。同时,还需要将 `main()` 方法中的唯一性判断、计算数组大小、创建新数组和打印数组的代码移至其他方法中,并进行相应修改。
键盘输入一个长度为10的int型数组,删除数组中重复出现的数据
可以使用双重循环遍历数组,对于每个元素,再遍历其后面的元素,如果有相同的元素,则将其删除。具体实现可以使用ArrayList来存储不重复的元素,最后再将其转换为数组返回。代码示例如下:
```
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5, 1, 2, 3, 6, 7};
int[] newArr = removeDuplicates(arr);
for (int i : newArr) {
System.out.print(i + " ");
}
}
public static int[] removeDuplicates(int[] arr) {
ArrayList<Integer> list = new ArrayList<>();
for (int i = ; i < arr.length; i++) {
boolean isDuplicate = false;
for (int j = i + 1; j < arr.length; j++) {
if (arr[i] == arr[j]) {
isDuplicate = true;
break;
}
}
if (!isDuplicate) {
list.add(arr[i]);
}
}
int[] newArr = new int[list.size()];
for (int i = ; i < list.size(); i++) {
newArr[i] = list.get(i);
}
return newArr;
}
}
```