加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
Test.java 6.53 KB
一键复制 编辑 原始数据 按行查看 历史
import java.util.Arrays;
public class Test {
//不规则二位数组
public static void main(String[] args) {
int[][] array = new int[2][];
array[0] = new int[] {11,22,33,44};
array[1] = new int[] {11,23,543,46,34,24};
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
System.out.print(array[i][j] + " ");
}
System.out.println();
}
}
//java中二维数组是一个特殊的一维数组
public static void main9(String[] args) {
int[][] array = {{1, 2, 3}, {4, 5, 6}};
System.out.println(Arrays.toString(array[0]));
System.out.println(Arrays.toString(array[1]));
System.out.println("------------------");
//三种打印二维数组的方式
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
System.out.print(array[i][j] + " ");
}
System.out.println();
}
System.out.println("------------------");
for (int [] ret : array) {
for (int x : ret) {
System.out.print((x + " "));
}
System.out.println();
}
System.out.println("------------------");
System.out.println(Arrays.deepToString(array));
}
public static void main8 (String[]args){
int[][] array = {{1, 2, 3}, {4, 5, 6}};
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(array[i][j] + " ");
}
System.out.println();
}
System.out.println("-------------------");
int[][] array2 = new int[][]{{1, 2, 3}, {4, 5, 6}};
System.out.println("-------------------");
int[][] array3 = new int[2][3];
}
//交换两个数
public static void swap ( int[] array, int i, int j){
int tmp = array[i];
array[i] = array[j];
array[j] = tmp;
}
//数组逆序
public static void reverse ( int[] array){
int left = 0;
int right = array.length - 1;
while (left < right) {
swap(array, left, right);
left++;
right--;
}
}
public static void main7 (String[]args){
int[] array = {1, 2, 3, 4, 5, 6, 7};
reverse(array);
System.out.println(Arrays.toString(array));
}
//判断连续的三个奇数
public static boolean func3 ( int[] array){
int count = 0;
for (int i = 0; i < array.length; i++) {
if (array[i] % 2 != 0) {
count++;
if (count == 3) {
return true;
}
} else {
count = 0;
}
}
return false;
}
public static void main6 (String[]args){
int[] array = {1, 2, 3, 4, 5, 5, 5, 6};
boolean ret = func3(array);
System.out.println(ret);
}
public static int finfMorenun2 ( int[] array){
int tmp = array[0];
int count = 0;
for (int i = 0; i < array.length; i++) {
if (array[i] == tmp) {
count++;
} else if (array[i] != tmp) {
count--;
}
if (count == 0) {
tmp = array[i];
count++;
}
}
return tmp;
}
//多数元素
public static int finfMorenun1 ( int[] array){
Arrays.sort(array);
return array[array.length / 2];
}
public static void main5 (String[]args){
int[] array = {1, 2, 3, 4, 6, 3, 3, 3, 3, 3, 5};
int ret1 = finfMorenun1(array);
System.out.println(ret1);
int ret2 = finfMorenun2(array);
System.out.println(ret2);
}
public static void bubbleSort ( int[] array){
boolean flg = false;
for (int i = 0; i < array.length - 1; i++) {
for (int j = 0; j < array.length - 1 - i; j++) {
if (array[j] > array[j + 1]) {
int tmp = array[j];
array[j] = array[j + 1];
array[j + 1] = tmp;
flg = true;
}
}
if (flg == false) {
return;
}
}
}
//冒泡排序
public static void main4 (String[]args){
int[] array = {10, 2, 3, 7, 5, 6};
bubbleSort(array);
System.out.println(Arrays.toString(array));
}
public static void main3 (String[]args){
int[] array = {1, 3, 2, 4, 7, 5};
Arrays.sort(array);
//在Java中提供了求二分查找的方法
System.out.println(Arrays.binarySearch(array, 7));
}
public static int func2 ( int[] array, int key){
int left = 0;
int right = array.length - 1;
while (left <= right) {
int mid = (left + right) / 2;
if (array[mid] < key) {
left = mid + 1;
} else if (array[mid] > key) {
right = mid - 1;
} else {
return mid;
}
}
return -1;
}
//二分查找
public static void main2 (String[]args){
int[] array = {1, 2, 3, 4, 5, 6, 7};
System.out.println(func2(array, 4));
}
//将一个数组奇数放在偶数的前面
public static void func1 ( int[] array){
int left = 0;
int right = array.length - 1;
while (left < right) {
while (left < right && array[left] % 2 != 0) {
left++;
}
while (left < right && array[right] % 2 == 0) {
right--;
}
int tmp = array[left];
array[left] = array[right];
array[right] = tmp;
}
}
public static void main1 (String[]args){
int[] array = {1, 2, 3, 4, 5, 6};
func1(array);
System.out.println(Arrays.toString(array));
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化