熟悉JAVA的集合框架,熟练掌握以下接口和类的使用
发布网友
发布时间:2022-04-26 18:21
我来回答
共2个回答
热心网友
时间:2022-05-05 00:59
public class ArrayTest {
static int b[];
public static void main(String[] args){
int a[] = {1,2,3,4,5,6,7,8,9,10};
b = new int[a.length];
//for循环拷贝
System.out.println("for循环拷贝的数组B");
for(int i = 0;i<a.length;i++){
b[i] = a[i] ;
}
printArray(b);
//API拷贝
System.out.println("API拷贝的数组B");
b = new int[5];
System.arraycopy(a, 3, b, 0, 5);
printArray(b);
}
public static void printArray(int a[]){
for(int i = 0;i<a.length;i++){
System.out.print(a[i]+" ");
}
System.out.println();
}
}
结果:
for循环拷贝的数组B
1 2 3 4 5 6 7 8 9 10
API拷贝的数组B
4 5 6 7 8
2排序:
import java.util.Arrays;
public class UpSort {
public static void main(String[] args) {
int a[] = { 2, 5, 7, 9, 2, 12, 57, 8, 122, 4 };
//排序前:
System.out.println("排序前");
print(a);
Arrays.sort(a);
//排序后:
System.out.println("排序后");
print(a);
}
public static void print(int a[]) {
for (int i = 0; i < a.length; i++) {
System.out.print(a[i]+" ");
}
System.out.println();
}
}
结果:
排序前
2 5 7 9 2 12 57 8 122 4
排序后
2 2 4 5 7 8 9 12 57 122
热心网友
时间:2022-05-05 02:17
//(1)
public class CopyArray
{
public static void main(String[]args)
{
int[] a ={1,2,3,4,5,6,7,8,9,10 };
int b[] = new int[10];
for(int i = 0; i <a.length;i++)
{
b[i] = a[i];
System.out.println(b[i]);
}
}
}
//(2)
public class CopyArray2
{
public static void main(String[]args)
{
int[] a ={1,2,3,4,5,6,7,8,9,10 };
int b[] = new int[10];
for(int i = 0; i <a.length;i++)
{
b[i] = a[i];
if((b[i]>2)&&(b[i]<8))
{
System.out.print(b[i]+",");
}
}
}
}
热心网友
时间:2022-05-05 00:59
public class ArrayTest {
static int b[];
public static void main(String[] args){
int a[] = {1,2,3,4,5,6,7,8,9,10};
b = new int[a.length];
//for循环拷贝
System.out.println("for循环拷贝的数组B");
for(int i = 0;i<a.length;i++){
b[i] = a[i] ;
}
printArray(b);
//API拷贝
System.out.println("API拷贝的数组B");
b = new int[5];
System.arraycopy(a, 3, b, 0, 5);
printArray(b);
}
public static void printArray(int a[]){
for(int i = 0;i<a.length;i++){
System.out.print(a[i]+" ");
}
System.out.println();
}
}
结果:
for循环拷贝的数组B
1 2 3 4 5 6 7 8 9 10
API拷贝的数组B
4 5 6 7 8
2排序:
import java.util.Arrays;
public class UpSort {
public static void main(String[] args) {
int a[] = { 2, 5, 7, 9, 2, 12, 57, 8, 122, 4 };
//排序前:
System.out.println("排序前");
print(a);
Arrays.sort(a);
//排序后:
System.out.println("排序后");
print(a);
}
public static void print(int a[]) {
for (int i = 0; i < a.length; i++) {
System.out.print(a[i]+" ");
}
System.out.println();
}
}
结果:
排序前
2 5 7 9 2 12 57 8 122 4
排序后
2 2 4 5 7 8 9 12 57 122
热心网友
时间:2022-05-05 02:17
//(1)
public class CopyArray
{
public static void main(String[]args)
{
int[] a ={1,2,3,4,5,6,7,8,9,10 };
int b[] = new int[10];
for(int i = 0; i <a.length;i++)
{
b[i] = a[i];
System.out.println(b[i]);
}
}
}
//(2)
public class CopyArray2
{
public static void main(String[]args)
{
int[] a ={1,2,3,4,5,6,7,8,9,10 };
int b[] = new int[10];
for(int i = 0; i <a.length;i++)
{
b[i] = a[i];
if((b[i]>2)&&(b[i]<8))
{
System.out.print(b[i]+",");
}
}
}
}