1、创建一个char类型的26个元素的数组,分别放置A-Z。使用for循环访问所有元素并打印出来
public class Test06 {
public static void main(String[] args) {
char[] array = new char[26];
char j = 65;
for (int i = 0; i < array.length; i++) {
if (array[i]==0){
array[i]=j;
}
j++;
}
for (int i = 0; i < array.length; i++) {
System.out.print(array[i]+" ");
}
}
}
2、数组赋值:创建一个char类型的36个元素的数组,前26个元素放置’A’-‘Z’, 后10个元素放置’0’-‘9’。使用for循环访问所有元素并打印出来。
public class TestArr02 {
public static void main(String[] args) {
char[] arr1 = new char[26];
char j = 65; //A
char k = 48; //0
char[] arr2 = new char[10];
char[] arr3 = new char[arr1.length+arr2.length];
for (int i = 0; i < arr1.length; i++) {
if(arr1[i]==0){
arr1[i]= j;
}
j++;
}
for (int i = 0; i < arr2.length; i++) {
if(arr2[i]==0){
arr2[i]= k;
}
k++;
}
// 给数组前26个元素赋值
for (int i = 0; i < arr1.length; i++) {
if(arr3[i]==0){
arr3[i] = arr1[i];
}
}
// 给数组后10个元素赋值
for (int i = 0; i < arr2.length; i++) {
if(arr3[arr1.length+i]==0){
arr3[arr1.length+i]=arr2[i];
}
}
// 合并数组
for (int i = 0; i < arr3.length; i++) {
System.out.print(arr3[i]+" ");
}
}
}
1、该数组中的所有偶数元素排列到奇数元素前 偶数元素从大到小排序,奇数元素从小到大排序
int[] array = { 6, 2, 7, 7, 0, 9, 8, 5, 6, 1 };
public class TestArr03 {
public static void main(String[] args) {
int[] array = { 6, 2, 7, 7, 0, 9, 8, 5, 6, 1};
int oddNum = 0; //奇数
int evenNum = 0; //偶数
// 1、遍历数组,统计奇数和偶数的个数
for (int i = 0; i < array.length; i++) {
if(array[i]%2==0){
evenNum++;
}else {
oddNum++;
}
}
int[] array1 = new int[oddNum]; //新建一个数组,用于存储奇数元素
int[] array2 = new int[evenNum];//新建一个数组,用于存储偶数元素
int k = 0;
int j = 0;
// 2、遍历数组,将偶数的值存到数组array2
for (int i = 0; i < array.length; i++) {
if(array[i]%2==0){
array2[k]= array[i];
k++;
}
}
// 3、遍历数组,将奇数的值存到数组array1
for (int i = 0; i < array.length; i++) {
if(array[i]%2!=0){
array1[j]= array[i];
j++;
}
}
// 4、对偶数数组array2排序
int temp = 0;
for (int i = 0; i < array2.length-1; i++) {
for (int l = 0; l < array2.length-1-i; l++) {
if(array2[l+1]>array2[l]){
temp = array2[l+1];
array2[l+1] = array2[l];
array2[l] = temp;
}
}
}
// 5、对奇数数组array1排序
for (int i = 0; i < array1.length-1; i++) {
for (int l = 0; l < array1.length-1-i; l++) {
if(array1[l+1]<array1[l]){
temp = array1[l];
array1[l] = array1[l+1];
array1[l+1] = temp;
}
}
}
// 6、合并两个数组
int[] array3 = new int[array1.length+ array2.length];
for (int i = 0; i < array2.length; i++) {
array3[i] = array2[i];
}
for (int i = 0; i < array1.length; i++) {
array3[array1.length+i] = array1[i];
}
System.out.println(Arrays.toString(array3));
}
}
1、int[][] arr = new int[][] { { 3, 12, 7 }, { 5, 9, 0 }, { 8, 6 }, { 4 } };
public class TestArrSum {
public static void main(String[] args) {
int[][] arr = new int[][] { { 3, 12, 7 }, { 5, 9, 0 }, { 8, 6 }, { 4 } };
int sum = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
sum += arr[i][j];
}
}
System.out.println(sum);
}
}
int[] array = { 6, 2, 7, 7, 0, 9, 8, 5, 6, 1 };
public class Test07 {
public static void main(String[] args) {
int[] array = { 6, 2, 7, 7, 0, 9, 8, 5, 6, 1}; //原数组
int[] array2 = new int[array.length];
// 遍历数组,测试相同元素的位置
for (int i = 0; i < array.length-1; i++) {
for (int j = i+1; j < array.length;j++) {
if(array[i]==array[j]){
array2[j] =1;
}
}
}
System.out.println(Arrays.toString(array2));
// 统计数组不重复的个数
int count = 0;
for (int i = 0; i < array2.length; i++) {
if(array2[i]==0){
count++;
}
}
System.out.println("数组的不重复个数为:"+count);
int[] array3 = new int[count];
int index = 0;
for (int i = 0; i < array2.length; i++) {
if(array2[i]==0){
array3[index++]= array[i];
}
}
System.out.println("去除掉重复元素后的数组:");
System.out.println(Arrays.toString(array3));
}
}
int[] arrays = {1,2,3,4,5};
public class Test08 {
public static void main(String[] args) {
int[] arrays = {1,2,3,4,5};
int[] reverse = new int[arrays.length];
for (int i = 0,j = arrays.length-1; i < arrays.length; i++,j--) {
reverse[j] = arrays[i];
}
System.out.println(Arrays.toString(reverse));
}
}