# 有關陣列陣列 (Array)的問題java

String all[]=input.split(" "); int[] data=new int[all.length]; for(int i=0;i 我不知道是哪裡出了問題，請告訴我我的問題在哪裡，或者請告訴我應該要換哪一種方法會比較好?

1 6 3 12 6 18 2

Update:

String all[]=input.split(" ");

int[] data=new int[all.length];

for(int i=0;i

Update 2:

all.length;i++){

int temp=Integer.parseInt(all[i]);

data[i]+=temp;

}

System.out.println(data[1]);

Update 3:

Integer.parseInt

String.parseInt

Update 4:

Will Integer.parseInt different place with String.parseInt

Rating
• John
Lv 7
7 years ago

import java.util.ArrayList;

import java.util.List;

import java.util.Scanner;

public class Program {

public static void main(String[] args) {

Program app = new Program();

app.two(); // the solution

}

public void one() {

String input = "1 6 3 12 6 18";

String[] inputs = input.split("\\D");

int[] data = new int[inputs.length];

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

data[i] = Integer.parseInt(inputs[i]);

System.out.printf("%d%n", data[i]);

}

}

public void two() {

String input = read("Enter list of departure and arrival time? ");

// test data: "1 6 3 12 6 18 11 12 18 19 20 21";

String[] inputs = input.split("\\D");

List<Bus> buses = new ArrayList<Bus>(0);

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

int departureAt = Integer.parseInt(inputs[i]); // arrival at

int arrivalAt = Integer.parseInt(inputs[i + 1]); // departure at

boolean matched = false;

for (Bus bus : buses) {

matched = bus.isAvailable(departureAt);

if (matched) {

bus.setArrivalAt(arrivalAt);

bus.setDepartureAt(departureAt);

break;

}

}

if (!matched) {

}

}

System.out.printf("%d number of bus is needed%n", buses.size());

}

String result = "";

System.out.print(message);

Scanner keyboard = new Scanner(System.in);

result = keyboard.nextLine();

return result;

}

}

class Bus {

private int departureAt;

private int arrivalAt;

public Bus(int departureAt, int arrivalAt) {

setArrivalAt(arrivalAt);

setDepartureAt(departureAt);

}

public boolean isAvailable(int departureAt) {

return (departureAt >= this.arrivalAt);

}

public void setDepartureAt(int departureAt) {

this.departureAt = departureAt;

}

public void setArrivalAt(int arrivalAt) {

this.arrivalAt = arrivalAt;

}

}