Hi folks,

Right now I'm trying to learn about multidimensional arrays. I'm currently working on an assignment where I have to store data in said arrays. However, the input format is funky and surpasses my knowledge of how I can store it.

The problem is generally along these lines. X number of people are in a circle and Y is the minimum amount of coins they can have to stay in the circle. I get X and Y from the user, then it get's tricky:

The input has to come in the form "(balance) (number of people this person lends to) (first person he is lending to) (amount) (second person...etc) (amount)

So, for person number one, who starts out with 5 coins and lends to both person 2 and person 3, giving them 1 coin each. The input for this would look like this: 5 2 2 1 3 1

I don't know how to go about storing this in my array without knowing the size and directly assigning each one. Such as, the 1st person may lend to 3 people and the 4th may lend to none. My array will have no input for the [5][2] and higher indexes....

Any help you can offer me is appreciated. I am trying to understand this and continue on with the bigger part of my project for this semester. This is just a minor hold up and I'm sure it's just avoiding my guesses.

The code I have now:

public class banks {	
	public static void main(String[] args) {
	List<List<Integer>> array = new ArrayList<List<Integer>>();
	//Declare variables
	int banks;
	double limit;
	Scanner input = new Scanner(System.in);
	//Get input from the user
	System.out.println("Enter the number of banks");
	banks= input.nextInt();
	System.out.println("Enter the limit of a safe bank (in millions)");
	limit= input.nextDouble();
	//Declare array and assign size
	double[][] borrowers;
	borrowers = new double[(banks-1)][banks];


2D arrays are really an array of arrays.
If you think of a 2D array in row and column terms, each row can have a different number of columns, including 0.
For example:

int[][] twoD = new int[5][]; // Define 2D array with 5 rows, leave # columns undefined
  twoD[0] = new int[3];  // set size of first row to be 3 columns
  twoD[1] = new int[1];  // set size of second row
This article has been dead for over six months. Start a new discussion instead.