當前位置:首頁 » 編程語言 » javafor循環數組

javafor循環數組

發布時間: 2022-07-12 20:25:13

1. java裡面for增強循環怎麼同時輸出兩個數組

for(String str : array)
java的增強for循環左邊是參數類型,右邊是循環的數組,嚴格意義上兩個數組之間是沒有什麼關聯的,除非有一個數組的值跟另一個數組的下標有關聯。
前端的增強for循環可以做到同時輸出兩個數組,
for(var index in array)
因為前端的增強for左邊是數組的下標,右邊是循環的數組,所以只要循環長度大的那個數組再加一些判斷是可以同時輸出兩個數組的。
這是我的一些個人見解,希望對你會有幫助。

2. Java中如何在for循環語句中對 類對象數組 進行賦值

abstract class Employee{
abstract double earnings();
}

class YearWorker extends Employee{
double earnings(){
return 15000;
}

}
class MonthWorker extends Employee{
double earnings(){
return 1200;
}
}
class WeekWorker extends Employee{
double earnings(){
return 280;
}
}

class Company{
int i=0;
Employee[] employee=new Employee[3];//主要看這里,有什麼不懂的嗎?
public void getSalary(Employee employee1,Employee employee2,Employee employee3){//我這代碼寫得不是很好,其實可以直接傳個數組進了的,參數可以只寫一個employee
this.employee[0]=employee1;
this.employee[1]=employee2;
this.employee[2]=employee3;
}
public double getAllSalary(){
double sum=0;
for(i=0;i<3;i++)
sum+=employee[i].earnings();
return sum;
}
}

public class Application1{
public static void main(String[] args){
Company company=new Company();
company.getSalary(new YearWorker(),new MonthWorker(),new WeekWorker());
System.out.println(company.getAllSalary());
}
}

//我寫的一個例子,供你參考。

3. Java 用for循環向一個一維數組中添加數據

源代碼:

import java.util.Scanner;

public class addElement {

public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("輸入需要的數組大小:");
Scanner scan=new Scanner(System.in);
int n = scan.nextInt();//接受輸入的數組大小
int[]arr=new int[n];//定義一個大小為剛輸入的n的數組
System.out.println("輸入數組的每個元素:");
for(int i=0;i<arr.length;i++)
arr[i]=scan.nextInt();//依次輸入元素到arr[i]
System.out.println("數組的元素依次為:");
for(int i=0;i<arr.length;i++)
System.out.print(arr[i]+" ");
}

}

4. java中for或foreach遍歷數組問題。

自從Java5中引入了foreach語句之後,在循環遍歷數組、集合方面帶來了極大的方便,但是在用foreach的時候也要注意其與for循環是不能完全劃等號的。

在使用foreach遍歷數組集合的過程中一定要注意不要修改其內容,在Java程序中會進行報錯,但是在有些時候沒有錯誤提示,就會造成花很多時間找不到問題所在。

5. java如何循環輸出數組

Scanner reader=new Scanner(System.in);
int Input=reader.nextInt();
for(int n=1;n<=Input;n++){
vertices[n-1]= "C"+n.toString(); //改成這個試試 //帶權有向圖的頂點集合
}

6. java中for或foreach是如何遍歷數組的

String[]array={"1","2","3","4","5"};
//for循環
for(inti=0;i<array.length;i++){
System.out.println(array[i]);
}

//foreach不是java裡面的關鍵字,foreache循環一般是指這個
for(Stringstring:array){
System.out.println(string);
}

7. java兩個數組合並用for循環

//兩個數組
String[] str1 = {"a","b","c"};
String[] str2 = {"d","e","f"};
//創建一個要接收的數組
String[] str3= new String[str1.length+str2.length];
//先把第一個數組放進去
for(int x=0;x<str1.length;x++){

str3[x] = str1[x];
}
for(int y=0;y<str2.length;y++){
str3[str1.length+y]=str2[y];
}
for(int y=0;y<str3.length;y++){
System.out.println(str3[y] + " ");
}
如有幫助請採納(不懂請提問),可以看我主頁,歡迎來交流學習;

8. java數組 在for循環中

原因很簡單,它的作用就是用來計算循環的次數,當達到條件時退出或進入循環,試想下,如果循環中沒有條件,那豈不是要一直循環下去,那樣就時死循環,還有,你要分清楚I++ 和++i的區別

9. java使用for循環求數組(95,63,75,25,92)中的最小值並列印

int max = 0;
List<Integer> integerList = new ArrayList<>();
integerList.add(95);
integerList.add(63);
integerList.add(75);
integerList.add(25);
integerList.add(92);
for(int i=0;i<integerList.size();i++){
if(integerList.get(i)>max){
max = integerList.get(i);
}
}
System.out.println("最大值:" + max);

熱點內容
酒店管理系統資料庫設計 發布:2024-04-30 04:27:56 瀏覽:178
安卓在哪裡看imei 發布:2024-04-30 04:01:36 瀏覽:6
clc存儲 發布:2024-04-30 03:58:59 瀏覽:831
百錢白雞c語言 發布:2024-04-30 03:52:57 瀏覽:298
阿里雲伺服器外包 發布:2024-04-30 03:33:54 瀏覽:278
911標配的有哪些配置 發布:2024-04-30 03:18:38 瀏覽:160
如何訪問阿里雲伺服器多個數據盤 發布:2024-04-30 03:08:45 瀏覽:188
ldd3源碼 發布:2024-04-30 03:07:14 瀏覽:7
phpecho換行 發布:2024-04-30 02:21:51 瀏覽:905
高中ftp 發布:2024-04-30 01:51:48 瀏覽:874