public class MultiDimArr {
public static void main(String[] args) {
double[][]array2Dim={
{1.2, 2.5, 3.2, 2.4, 1.5},
{6.8, 3.7, 6.7, 8.9, 5.3},
{4.1, 1.2, 3.1, 4.2, 1.7}
};
}
private static void outArr2Dim(double[][]outArr)
{
for(int i=0;i<outArr.length;i++)
{
String outStr="";
for(int j=0;j<outArr[0].length;j++)
{
outStr=outStr+"\t"+outArr[i][j];
}
System.out.println(outStr);
}
}
}
2 Dimensional Arrays
Page 1 of 14 Replies - 657 Views - Last Post: 11 January 2008 - 06:28 AM
#1
2 Dimensional Arrays
Posted 10 January 2008 - 08:04 AM
Hi, can anyone see why this program isn't running. I've tried everything and am finding this topic quite hard. I'm trying to get the program to display the array contents in the console window. Please help.
Replies To: 2 Dimensional Arrays
#2
Re: 2 Dimensional Arrays
Posted 10 January 2008 - 08:07 AM
change:
for(int j=0;j<outArr[0].length;j++)
to
for(int j=0;j<outArr[i].length;j++)
You are always printing the first row.
You may also want to add: outArr2Dim(array2Dim); to your main method, but imagine that was just a copy and paste error.
for(int j=0;j<outArr[0].length;j++)
to
for(int j=0;j<outArr[i].length;j++)
You are always printing the first row.
You may also want to add: outArr2Dim(array2Dim); to your main method, but imagine that was just a copy and paste error.
#3
Re: 2 Dimensional Arrays
Posted 10 January 2008 - 08:13 AM
Thanks very much. My notes that I have don't explain anything.
#4
Re: 2 Dimensional Arrays
Posted 10 January 2008 - 08:30 AM
No problem, it is an easy mistake to make, I'm sure I've done it myself.
The idea was correct, the length of outArr[0] is the right length, it is just a static declaration over outArr[i] being dynamic across the rows.
The idea was correct, the length of outArr[0] is the right length, it is just a static declaration over outArr[i] being dynamic across the rows.
#5
Re: 2 Dimensional Arrays
Posted 11 January 2008 - 06:28 AM
On the same topic I am trying another program. It is meant to ask the user to input the data for each row in the array. Can anyone see why it is not working. Part of the problem is that I am not fully understanding what's happening. I take it the first method shouldn't be there? Thanks
public class MultiDimArr {
public static void main(String[] args) {
double[][]array2Dim={
{1.2, 2.5, 3.2, 2.4, 1.5},
{6.8, 3.7, 6.7, 8.9, 5.3},
{4.1, 1.2, 3.1, 4.2, 1.7}
};
getArrInput(array2Dim);
outArr2Dim(array2Dim);
copySquare(array2Dim);
}
private static void outArr2Dim(double[][]outArr)
{
for(int i=0;i<outArr.length;i++)
{
String outStr="";
for(int j=0;j<outArr[0].length;j++)
{
outStr=outStr+"\t"+outArr[i][j];
}
System.out.println(outStr);
}
}
private static void copySquare(double[][]sourceArr)
{
double[][]destArr=new double[sourceArr.length][sourceArr[0].length];
System.arraycopy(sourceArr,0,destArr,0,sourceArr.length);
for (int i=0;i<destArr.length;i++)
{
for(int j=0;j<destArr[0].length;j++)
{
destArr[i][j]*=destArr[i][j];
}
}
outArr2Dim(destArr);
}
private static void getArrInput(double[][]loadArr)
{
int noRows=loadArr.length;
int noCols=loadArr[0].length;
for(int i=0;i<noRows;i++)
{
String InpStr=JOptionPane.showInputDialog(null,
"Please input the elements for row "+i+" separated by commas",
"2 Dimensional Array Demo",
JOptionPane.QUESTION_MESSAGE);
StringTokenizer TokStr=new StringTokenizer(InpStr,",");
for(int j=0;j<noCols;j++)
{
loadArr[i][j]=Double.parseDouble(TokStr.nextToken());
}
}
}
}
This post has been edited by harmonica: 11 January 2008 - 06:33 AM
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote




|