I am having an output problem with my java code.
I am trying to implement this multiply matrix method and it compiles just fine. The only problem is my output. I seem to be getting the following:
---- Test Multiply Matrix ----
[[D#7f31245a
Should return C={{ 3, 2},{ 1, 1}}
Can someone please help me understand where I am going wrong here. Thanks!
Here is my source code:
public class Recommendation
{
public static double[][] multiplyMatrix(double[][] A, double[][] B)
{
int aRows = A.length;
int bRows = B.length;
int aColumns = A[0].length;
int bColumns = B[0].length;
if((aColumns != bRows))
{
return null;
}
else
{
double[][] C = new double[aRows][bColumns];
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
C[i][j] = 0;
}
}
for (int i = 0; i < aRows; i++)
{
for (int j = 0; j < bColumns; j++)
{
for (int k = 0; k < aColumns; k++)
{
C[i][j] += A[i][k] * B[k][j];
}
}
}
return C;
}
}
static double [][] A = {{ 1, 0, 2},
{ 0, 1, 1}};
static double [][] B = {{1, 2},
{ 0, 1},
{ 1, 0}};
public static void main(String[] argss)
{
// TEST multiplyMatrix
System.out.println(" ---- Test Multiply Matrix ---- ");
System.out.println(multiplyMatrix(A,B)); // should return C={{ 3, 2},{ 1, 1}}
System.out.println("Should return C={{ 3, 2},{ 1, 1}}");
System.out.println(" ");
}
}
You might want to use Arrays.toString from java.util.Arrays to print arrays.
Or, if you want your output to be a little more custom, you can iterator over the array.
Note that Arrays.toString alone won't help you, since your array is two dimensional.
It would still print something of the form : [[I#355d56d5, [I#2efd552, [I#4f9dfbff]
Instead, you can do something like this :
double[][] C = multiplyMatrix(A,B);
for (double[] subArray : C) {
System.out.print (Arrays.toString (subArray));
System.out.print (" , ");
}
System.out.println();
Or, you can use Arrays.deepToString(C) which will take care of the hierarchy for you.
#deepToString Returns a string representation of the "deep contents" of the
specified array. If the array contains other arrays as elements, the
string representation contains their contents and so on. This method
is designed for converting multidimensional arrays to strings.
You should use java.util.Arrays.deepToString(array) for multi-dimensional array.Currently you are printing Object reference's String representation.
You can use #replace method to replace[] with {}
//...
public static void main(String[] argss){
// TEST multiplyMatrix
System.out.println(" ---- Test Multiply Matrix ---- ");
double array[][] = multiplyMatrix(A,B);
String finalString = Arrays.deepToString(array)
.replace("[", "{")
.replace("]", "}");
System.out.println(finalString);
}//...
public static double[][] multiplyMatrix(double[][] A, double[][] B).
Here you are returning a double array. which is not a primitive type. So, the default toString() method of array will be used (Which prints classname#hashCode, hence the output). You have to use Arrays.toString() to print the values properly.
[[D#7f31245a means a 2D array of Double's followed by the hashcode of the actual object.
Your multiplyMatrix() method returns exactly this, but the toString() method invoked is that on Object which prints exactly this. You'll need to to use methods on Arrays class to prettyprint arrays.
Cheers,
You can use Arrays#deepToString():
System.out.println(Arrays.deepToString(multiplyMatrix(A,B)));
multiplyMatrix returns an array, which is an object, and in Java since each object has toString() method, the default is displaying the class name representation, then adding # sign and then the hashcode.
In order to better understand what's happening here, see the implementation of Arrays.deepToString.
Note that if you want more control on the output, e.g. filtering some arrays or change the way display them, you can have nested loops.
Related
When I run the below code it's returning same array as output. Can anyone tell me where I am wrong?
public class MoveZeroToend {
public static void main(String[] args) {
int[] arr = { 1, 3, 0, 2, 0, 2, 0 };
Move0Toend(arr);
}
static void Move0Toend(int[] arr) { // Code to move zeroes to end
int count = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] != 0) {
swap(arr[i], arr[count]);
count++;
}
}
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " "); // Print the array
}
}
static void swap(int a, int b) { // To swap
a = a + b;
b = a - b;
a = a - b;
}
}
on your swap method, you are not swapping the actual values of the objects you've passed, you are swapping between the values passed to the method but there is no result returned so nothing happens. you need to either do the swap on the actual objects - not in a method, or use another way for this. I would recommend googling "pass by value" and "pass by reference". I would also recommend adding a unit test or at least debug the program so you can validate your code is doing what you want.
Your swap method doesn't return any values, nor does it change the values in the reference of the objects passed. To fix this you can either return two values from your swap method (a and b) or you could do it not in a method, that way it would directly affect the objects.
Just for a little more explanation, the variables a and b in your swap method are local to the swap method, changing these would not affect any other variables, even if they were also named the same, and as your method is a void it can't return anything.
Hope this helps :)
Your swap() method isn't performing any operation on your array, you are just passing two values a and b and swapping them but no operation is being performed on your array.
Instead of passing these two values to your swap() method you can directly swap them inside your for loop as below:
for(int i=0;i<arr.length;i++){
if(arr[i]!=0){
int temp = arr[i];
arr[i] = arr[count];
arr[count] = temp;
count++;
}
}
This question already has answers here:
Comparing arrays in JUnit assertions, concise built-in way?
(8 answers)
Closed 4 years ago.
This is probably a really bad way of writing code but heres a program that calculates the total of each row and print it all in brackets.
public static int[] rowsSums(int[][] array) {
int[][] numArray = {
{3, -1, 4, 0},
{5, 9, -2, 6},
{5, 3, 7, -8}
};
int rowTotal = 0;
int row2Total = 0;
int row3Total = 0;
for (int i = 0; i < numArray[0].length; i++) {
rowTotal += numArray[0][i];
row2Total += numArray[1][i];
row3Total += numArray[2][i];
}
System.out.println("(" + rowTotal + "," + row2Total + "," + row3Total + ")");
return null;
}
The output without JUnit is:
(6,18,7)
I am in the process of testing this with JUnit and my code for this:
#Test
public void rowsSums() {
int [] i = new int [] {6, 18, 7};
assertEquals(i, Exercise2.rowsSums(numArray));
}
Yes, I know my output is not supposed to be null because JUnit hates it. What other variable can I return without making JUnit fail or spit an error?
I have to keep these as it is
public static int[] rowsSums(int[][] array) {
int[][] numArray = {
UPDATE: No matter what I try, JUnit always comes up with this error PrntScrn of Error
To return the int[] containing the sums, you'd do
return new int[] { rowTotal, row2Total, row3Total };
That's something you can assert as well then
assertArrayEquals(i, Exercise2.rowsSums(numArray));
Note that it is good practice to separate calculation and output, ie you should move the System.out.println to another function accepting the returned array as a parameter.
If your method is designed to just print a message and not return anything, you should declare its return type to be void.
public static void rowsSums(int[][] array) { ...
Then just remove the return statement.
However, based on your test case, it looks like you want to return an array containing the values that you calculated. Instead of having three internal variables to hold totals (rowTotal, row2Total, and row3Total), those variables could be combined into one array where the totals are stored, then returned.
I think that your method should return the computation performed and not only print a message otherwise you would not have a clear way to test your method.
Besides method names should be verbs/actions and not common names.
So sum() would be clearer.
You could return the sum array of each row in your method and assert the result in your unit test :
public static int[] sum(int[][] array) {
...
return new int[] {rowTotal, row2Total, row3Total};
}
And in your test :
int[] sumExpected = ...
Assert.assertArrayEquals(sumExpected, Exercise2.sum(numArray));
If you will return array as a result you will be able to use it in your jUnit test and compare expected result with actual.
System.out.println("(" + rowTotal + "," + row2Total + "," + row3Total + ")");
int [] result = new int[3];
result[0] = rowTotal;
result[1] = row2Total;
result[2] = row3Total;
return result;
Why do you pass int[][] array to the method, but literally never do anything with it at all?... I'm going to assume you want to use that array structure you passed in order to count each of it's arrays (rows). You also should never mix output with functions. You should return the String, then print it in your main method. As #bill-the-Lizard said, you want to have an array in which holds the row's values and then you would just return an int[] array which contains the rows' counts. You would then have a for loop in your current one which would input the counts into their corresponding array key.
int[][] numArray = {
{3, -1, 4, 0},
{5, 9, -2, 6},
{5, 3, 7, -8}
};
public static int[] rowsSums(int[][] array) {
int[] sum = new int[array.length];
for (int i=0;i<sum.length; i++)
sum[i] = 0;
for (int i = 0; i < sum.length; i++) {
for(int j=0; j< array[i].length; j++)
sum[i] += array[i][j];
}
return sum;
}
int[] result = rowsSums(numArray);
I have used the code below to create and populate an array, however, when it comes to printing the array I do not get the result I expect while using the Arrays.toString() function.
Rather than printing
newArray: [2, 4, 6]
newArray: [8, 10, 12]
etc..
it prints
newArray: [[I#15db9742, [I#6d06d69c, [I#7852e922, [I#4e25154f]
newArray: [[I#15db9742, [I#6d06d69c, [I#7852e922, [I#4e25154f]
etc..
The code:
public static void main(String[] args) {
int[][] newArray = new int[4][3];
int number = 2;
for (int rowCounter = 0; rowCounter < newArray.length; rowCounter++) {
for (int colCounter = 0; colCounter < newArray[rowCounter].length; colCounter++) {
newArray[rowCounter][colCounter] = number;
number += 2;
}
System.out.println("newArray: " + Arrays.toString(newArray));
}
}
Any help with this would be much appreciated.
You can use deepToString instead of toString:
System.out.println(Arrays.deepToString(newArray));
Try
Arrays.deepToString(newArray)
With the result you desire, make sure the array you are declaring is one-dimensional. You declared a two dimensional array which you are not using correctly.
Change int[][] newArray to int[] newArray
when you call toString(Object[] a) internally it will call Object.toString method.
(obj == null) ? "null" : obj.toString();
If you want to print all element then as mentioned you have to deepToString instead of toString method.Internally it will for a object type array will loop through the array and convert to string.
For an example every array element in your case is int array will call toString.
if (eClass.isArray()) {
if (eClass == int[].class)
buf.append(toString((int[]) element));
I'm learning and understanding Java now, and while practising with arrays I had a doubt. I wrote the following code as an example:
class example
{
public static void main(String args[])
{
String a[] = new String[] {"Sam", "Claudia", "Josh", "Toby", "Donna"};
int b[] = new int[] {1, 2, 3, 4, 5};
for(int n=0;n<5;n++)
{
System.out.print (a[n] + "...");
System.out.println (b[n]);
}
System.out.println (" ");
java.util.Arrays.sort(a);
for(int n=0;n<5;n++)
{
System.out.print (a[n] + "...");
System.out.println (b[n]);
}
}
In a nutshell, this class created two arrays with five spaces each. It fills one with names of characters from the West Wing, and fills the other with numbering from one to five. We can say that the data in these two strings corresponds to each other.
Now, the program sorts the array with the names in it using Arrays.sort(). After printing the array again, you can see that while the names are now in alphabetical order, the numbers do not correspond anymore as the second array is unchanged.
How can I shuffle the contents of the second array to match the sort requirements of the first? The solution must also be flexible to allow for changes in the scope and size of the program. Please do not post any answers asking me to change my methodology with the arrays, or propose a more 'efficient' way of doing things. This is for educational purposed and I'd like a straight solution to the example code provided. Thanks in advance!
EDIT: I do NOT want to create an additional class, however I think some form of sorting through nested loops might be an option instead of Arrays.sort().
Below is the code without using any Map Collection, but if you want to use Map then it becomes very easy. Add both the arrays into map and sort it.
public static void main(String args[]) {
String a[] = new String[] {
"Sam", "Claudia", "Josh", "Toby", "Donna"
};
int b[] = new int[] {
1, 2, 3, 4, 5
};
for (int n = 0; n < 5; n++) {
System.out.print(a[n] + "...");
System.out.println(b[n]);
}
System.out.println(" ");
//java.util.Arrays.sort(a);
/* Bubble Sort */
for (int n = 0; n < 5; n++) {
for (int m = 0; m < 4 - n; m++) {
if ((a[m].compareTo(a[m + 1])) > 0) {
String swapString = a[m];
a[m] = a[m + 1];
a[m + 1] = swapString;
int swapInt = b[m];
b[m] = b[m + 1];
b[m + 1] = swapInt;
}
}
}
for (int n = 0; n < 5; n++) {
System.out.print(a[n] + "...");
System.out.println(b[n]);
}
}
Some people propose making a product type. That is feasible only if the amount of elements is small. By introducing another object you add object overhead (30+ bytes) for each element and a performance penalty of a pointer (also worsening cache locality).
Solution without object overhead
Make a third array. Fill it with indices from 0 to size-1. Sort this array with comparator function polling into the array according to which you want to sort.
Finally, reorder the elements in both arrays according to indices.
Alternative solution
Write the sorting algorithm yourself. This is not ideal, because you might make a mistake and the sorting efficiency might be subpar.
You have to ZIP your two arrays into an array which elements are instances of a class like:
class NameNumber
{
public NameNumber(String name, int n) {
this.name = name;
this.number = n;
}
public String name;
public int number;
}
And sort that array with a custom comparator.
Your code should be something like:
NameNumber [] zip = new NameNumber[Math.min(a.length,b.length)];
for(int i = 0; i < zip.length; i++)
{
zip[i] = new NameNumber(a[i],b[i]);
}
Arrays.sort(zip, new Comparator<NameNumber>() {
#Override
public int compare(NameNumber o1, NameNumber o2) {
return Integer.compare(o1.number, o2.number);
}
});
You should not have two parallel arrays. Instead, you should have a single array of WestWingCharacter objects, where each object would have a field name and a field number.
Sorting this array by number of by name would then be a piece of cake:
Collections.sort(characters, new Comparator<WestWingCharacter>() {
#Override
public int compare(WestWingCharacter c1, WestWingCharacter c2) {
return c1.getName().compareTo(c2.getName();
}
});
or, with Java 8:
Collections.sort(characters, Comparator.comparing(WestWingCharacter::getName));
Java is an OO language, and you should thus use objects.
What you want is not possible because you don't know internally how Arrays.sort swap the elements in your String array, so there is no way to swap accordingly the elements in the int array.
You should create a class that contains the String name and the int position as parameter and then sort this class only with the name, providing a custom comparator to Arrays.sort.
If you want to keep your current code (with 2 arrays, but this not the ideal solution), don't use Arrays.sort and implement your own sorting algorithm. When you swap two names, get the index of them and swap the two integers in the other array accordingly.
Here is the answer for your query.
public class Main {
public static void main(String args[]){
String name[] = new String[] {"Sam", "Claudia", "Josh", "Toby", "Donna"};
int id[] = new int[] {1, 2, 3, 4, 5};
for ( int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
int dtmp=0;
String stmp=null;
if (id[i] > id[j]) {
dtmp = rate[i];
id[i] = id[j];
id[j] = dtmp;
stmp = name[i];
name[i]=name[j];
name[j]=stmp;
}
}
}
System.out.println("Details are :");
for(int i=0;i<n;i++){
System.out.println(name[i]+" - "+id[i]);
}
}
}
The same solution, as a function that can be added to some utils class:
public static final boolean INCREASING = true;
public static final boolean DECREASING = false;
#SuppressWarnings("unchecked")
public static <T extends Comparable, U extends Object> void bubbleSort(ArrayList<T> list1, ArrayList<U>list2, boolean order) {
int cmpResult = (order ? 1 : -1);
for (int i = 0; i < list1.size() - 1; i++) {
for (int j = 0; j <= i; j++) {
if (list1.get(j).compareTo(list1.get(j+1)) == cmpResult) {
T tempComparable = list1.get(j);
list1.set(j , list1.get(j + 1));
list1.set(j + 1 , tempComparable);
U tempObject = list2.get(j);
list2.set(j , list2.get(j + 1));
list2.set(j + 1 , tempObject);
}
}
}
}
The arrays are not linked in any way. Like someone pointed out take a look at
SortedMap http://docs.oracle.com/javase/7/docs/api/java/util/SortedMap.html
TreeMap http://docs.oracle.com/javase/7/docs/api/java/util/TreeMap.html
import java.util.*;
class mergeArrays2
{
public static void main(String args[])
{
String a1[]={"Sam", "Claudia", "Josh", "Toby", "Donna"};
Integer a2[]={11, 2, 31, 24, 5};
ArrayList ar1=new ArrayList(Arrays.asList(a1));
Collections.sort(ar1);
ArrayList ar2=new ArrayList(Arrays.asList(a2));
Collections.sort(ar2);
System.out.println("array list"+ar1+ " "+ar2);
}
}
I have no problem calling methods that require String or int inputs. For example:
return stringMethod("Hello World");
return intMethod(1,2,3);
but I'm having an issue with the syntax when calling a method which requires array of ints for the input. The syntax I use to call the method countEvens in the code below is not working.
public class _01_countEvens{
public static void main(String[] args){
return countEvens({2,4,6,7});
}
}
public int countEvens(int[] nums){
int result = 0;
for(int x = 0; x < nums.length; x++){
if(nums[x] % 2 == 0) result++;
}
return result;
}
}
This syntax
{2,4,6,7}
is the array creation syntax and can only be used in array creation expressions
new int[]{2,4,6,7}
Read the official Java tutorial on Arrays here.
Either change your method header to:
public int countEvents(int... nums)
And remove the { and } in the call to countEvents,
Or pass: new int[]{2, 4, 6, 7} as an argument.
Array:
int[] a = {0,1,2,3,4,5};
Double Array:
int[][] a2 = {
{0,1,2}
{3,4,5}
};
From there on, just add arrays inside each array. You shouldn't have to make that many dimensions though.