導航:首頁 > 編程語言 > java數組習題

java數組習題

發布時間:2022-08-05 06:08:52

A. java 數組題


import java.util.ArrayList;

import java.util.Arrays;

import java.util.List;

public class Test {

public static void main(String[] args) {

String[] arr = new String[]{"itcast","itheima","t","weixin","fubao"};

String str = "it";

String[] result = filter(arr,str);

System.out.println(Arrays.toString(result));

}

public static String[] filter(String[] arr,String str){

List<String> list = new ArrayList<>();

for(int i = 0; i < arr.length; i++){

if(arr[i].contains(str)){

list.add(arr[i]);

}

}

String[] result = new String[list.size()];

list.toArray(result);

return result;

}

}

B. 一道java數組習題求助!!!!

先算出長度,再填。

import java.util.Arrays;
public class Test{
public static void main(String[] args){
int a[][]={
{17,23,1,5},
{8,9},
{34,35,56}
};
int b[]=new int[a.length+a[0].length+a[1].length+a[2].length];
int offset=0;
for(int e[]:a){
b[offset++]=e.length;
System.array(e, 0, b, offset, e.length);
offset+=e.length;
}
System.out.println(Arrays.toString(b));
}
}
=====
[4, 17, 23, 1, 5, 2, 8, 9, 3, 34, 35, 56]

C. java數組題

參考代碼

classStudent{
privateStringname;
privateStringage;
publicStudent(){//構造方法
}
publicStudent(Stringname,Stringage){//有參構造方法
this.name=name;
this.age=age;
}
publicStringtoString(){
return"Student[name="+name+",age="+age+"]";
}
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
this.name=name;
}
publicStringgetAge(){
returnage;
}
publicvoidsetAge(Stringage){
this.age=age;
}

}
publicclassTest{
publicstaticvoidmain(String[]args){
Student[]ss=newStudent[3];
Stringinfo="Tom#15,jack#16,Dava#20";
String[]temp=info.trim().split(",");//用逗號切割成3段
for(inti=0;i<temp.length;i++){
String[]s1=temp[i].split("#");//用#切割成2部分
Stringname=s1[0];
Stringage=s1[1];
ss[i]=newStudent(name,age);//創建對象並存入學生數組
}

for(Students:ss){//遍歷列印
System.out.println(s);
}
}
}

測試

Student[name=Tom,age=15]
Student[name=jack,age=16]
Student[name=Dava,age=20]

D. Java數組的一些題目求解謝謝

  1. D 數組中存儲的數據類型需一致;

  2. C 根據定義數組的類型為int(整型),因此表示數組中有100個整數;

  3. B 訪問數組元素是從0開始訪問,因此在數組a[10]中最後一個元素為a[9];

  4. 對於數組a,可以使用a.length表示數組的長度,即數組的元素個數。

E. JAVA數組編程

publicstaticvoidmain(String[]args){
int[]arr=newint[10];
intlength=arr.length;
for(inti=0;i<length;i++){
arr[i]=(int)(100*Math.random());
if(i==0){
arr[i]=100;
}elseif(i==length-1){
arr[i]=200;
}
}
for(inti:arr){
System.out.println(i);
}
}

F. java數組題

package com.linc.;

import java.util.Random;
import java.util.Scanner;

/**
* 數組轉置
*
* @author Lin.C
* @date 2020/6/7 13:48
*/
public class Answer001 {

/**
* Main
* @param args
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Please enter m:");
int m = Integer.parseInt(sc.next());
System.out.println("Please enter n:");
int n = Integer.parseInt(sc.next());

printMatrix(m, n);
}

/**
* 輸入整數m,n
* 1. 建立一個擁有m行,n列的矩陣a, 矩陣a中元素的值為隨機產生的100以內的整數;輸出矩陣a
* 2. 並輸出經過轉置後的具有n行m列的矩陣b.
* @param m
* @param n
*/
private static void printMatrix(int m, int n) {

System.out.println("-------原數組--------");
Random random = new Random();
int a[][] = new int[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
a[i][j] = random.nextInt(100);
System.out.print(a[i][j] + " ");
}
System.out.println();
}

int b[][] = new int[n][m];
System.out.println("-------轉置後--------");
for (int j = 0; j < n; j++) {
for (int i = 0; i < m; i++) {
b[j][i] = a[i][j];
System.out.print(a[i][j] + " ");
}
System.out.println();
}
}
}

【運行結果】:
Please enter m:
3
Please enter n:
4
-------原數組--------
38 9 6 36
39 31 34 26
67 26 70 82
-------轉置後--------
38 39 67
9 31 26
6 34 70
36 26 82

G. Java數組,下列習題,高手能給下代碼嗎

你好,演算法代碼如下:


publicclassTestArray{
/**
*定義一個可以存放五個人的三門課程成績的二維數組,數組名為scores,並賦初值(即學生的成績)。
*(1)輸出五個人的成績(每個人佔一行)
*(2)另定義一個數組avg,用來存放五個人的平均成績,計算出平均成績並輸出。
*(3)求出每門課程的平均成績
*(4)求出每門課程的最高分
*/
publicstaticvoidmain(String[]args){
double[][]scores=newdouble[5][3];//定義數組
scores[0][0]=65;scores[0][1]=75;scores[0][2]=85;//第一個學生的成績
scores[1][0]=66.5;scores[1][1]=83;scores[1][2]=80.5;//第二個學生的成績
scores[2][0]=94;scores[2][1]=68.5;scores[2][2]=69;//第三個學生的成績
scores[3][0]=86;scores[3][1]=72.5;scores[3][2]=95;//第四個學生的成績
scores[4][0]=79;scores[4][1]=88;scores[4][2]=74;//第五個學生的成績
//(1)輸出五個人的成績(每個人佔一行)
for(inti=0;i<5;i++){
System.out.print("第"+(i+1)+"個人的三門成績是:");
for(intj=0;j<3;j++){
System.out.print(scores[i][j]+"");
}
System.out.println();
}
//(2)另定義一個數組avg,用來存放五個人的平均成績,計算出平均成績並輸出。
double[]avg=newdouble[5];
for(inti=0;i<5;i++){
avg[i]=(scores[i][0]+scores[i][1]+scores[i][2])/3;
System.out.println("第"+(i+1)+"個人的平均成績是:"+avg[i]);
}

//(3)求出每門課程的平均成績
double[]avgScore=newdouble[3];
for(inti=0;i<3;i++){
avgScore[i]=(scores[0][i]+scores[1][i]+scores[2][i]+scores[3][i]+scores[4][i])/5;
System.out.println("第"+(i+1)+"科的平均成績是:"+avgScore[i]);
}

//(4)求出每門課程的最高分
double[]highScore={0,0,0};
for(inti=0;i<3;i++){
for(intj=0;j<5;j++){
if(highScore[i]<scores[j][i]){
highScore[i]=scores[j][i];
}
}
System.out.println("第"+(i+1)+"科的最高成績是:"+highScore[i]);
}
}
}

運行結果:

第1個人的三門成績是: 65.0 75.0 85.0
第2個人的三門成績是: 66.5 83.0 80.5
第3個人的三門成績是: 94.0 68.5 69.0
第4個人的三門成績是: 86.0 72.5 95.0
第5個人的三門成績是: 79.0 88.0 74.0
第1個人的平均成績是:75.0
第2個人的平均成績是:76.66666666666667
第3個人的平均成績是:77.16666666666667
第4個人的平均成績是:84.5
第5個人的平均成績是:80.33333333333333
第1科的平均成績是:78.1
第2科的平均成績是:77.4
第3科的平均成績是:80.7
第1科的最高成績是:94.0
第2科的最高成績是:88.0
第3科的最高成績是:95.0

閱讀全文

與java數組習題相關的資料

熱點內容
證券技術分析pdf 瀏覽:777
linux命令連接oracle 瀏覽:200
墊江停車收費樁怎麼上App 瀏覽:133
好興動app還款怎麼登錄不上去了 瀏覽:665
鄭州雲伺服器託管 瀏覽:722
伺服器地址跟蹤 瀏覽:980
免費google雲伺服器 瀏覽:516
摘譯和編譯的英文 瀏覽:359
熱泵壓縮機選型 瀏覽:121
op手機微信加密如何解除 瀏覽:386
如何在王牌戰爭找到高爆率伺服器 瀏覽:13
江浙小學語文輔導課用什麼APP 瀏覽:99
新夢幻大陸伺服器地址 瀏覽:241
網吧伺服器怎麼更換壁紙 瀏覽:530
linux命令方法 瀏覽:332
linux下載freetype 瀏覽:123
程序員入駐平台 瀏覽:327
程序員大戰外掛 瀏覽:745
html實例教程pdf 瀏覽:157
linux命令開放所有許可權 瀏覽:575