Question: How do I print out (in my toString method) a compressed array (2d matrix) from an array such as: Output: 20 14 20 12 35

How do I print out (in my toString method) a compressed array (2d matrix) from an array such as:

Output:

20

14 20

12 35 46

15 35 67 89

********************

Using this code:

import java.util.Arrays; public class CompressedArray { private int origArraySize; private double array[]; public CompressedArray (double[][] aArray) { int n = aArray.length; int total = (n * (n - 1)) / 2; int i, j, k = 0; origArraySize = total; array = new double[total]; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { if (i > j) array[k++] = aArray[i][i]; } } } public int getLength () { return origArraySize; } public double getElement(int i) { return array[i]; } @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } final CompressedArray other = (CompressedArray) obj; if (this.origArraySize != other.origArraySize ) { return false; } if (!Arrays.equals(this.array, other.array)) { return false; } return true; } @Override public String toString() {

String t = ""; for (int i = 0; i < this.origArraySize; i++) System.out.println(array[i]); //t += String.format("%8.2f", array[i]); return "CompressedArray: " + "Original Array Size=" + origArraySize + " Array=" +t;

} }

Text file associated with this example:

city name,x,y Calgary 367 661 Edmonton 375 625 Montreal 802 737 Ottawa 780 740 Toronto 750 762 Vancouver 273 683 Winnipeg 546 670

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!