LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-09-2007, 05:04 AM   #1
Garda
Member
 
Registered: May 2005
Distribution: Debian
Posts: 83
Blog Entries: 2

Rep: Reputation: 15
JAVA: creating a multidimensional array


i have 2 examples trying to do the same thing

the first one works fine

Code:
class working
{
	public static void main(String args[])
	{
		int[][] numbers = 
		{
			{1,2,3,4},
			{5,6},
			{7,8,9,10,11,12}
		};	
	}
}
the second one doesn't

Code:
class numbers
{
	public static void main(String args[])
	{
		int numbers[][] = new int[3][];
		int numbers[0][] = {1,2,3,4};
		int numbers[1][] = {5,6};
		int numbers[2][] = {7,8,9,10,11,12};
		
	}
}
what's wrong with the second one?

also, in a 2D array, does the second dimension have to be the same type as the container array?
what i mean by this, is something like the following valid?

Code:
long numbers[][] = new long[2];
numbers[0] = new char[3];
numbers[1] = new long[3];
thanks in advance
 
Old 08-09-2007, 09:20 AM   #2
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 43
Quote:
Originally Posted by Garda
Code:
class numbers
{
	public static void main(String args[])
	{
		int numbers[][] = new int[3][];
		int numbers[0][] = {1,2,3,4};         // <-- variable redeclaration
		int numbers[1][] = {5,6};             // <-- same here
		int numbers[2][] = {7,8,9,10,11,12};  // <-- same here
		
	}
}
what's wrong with the second one?
You are redeclaring the variable "numbers". Remove the "int" at the beginning of the line.

Quote:
Originally Posted by Garda
also, in a 2D array, does the second dimension have to be the same type as the container array?
what i mean by this, is something like the following valid?
Since you declare it to be a double-array of longs, everything has to be a long. If you want arrays to hold different types, you have to use objects.
 
Old 08-09-2007, 09:44 AM   #3
Garda
Member
 
Registered: May 2005
Distribution: Debian
Posts: 83

Original Poster
Blog Entries: 2

Rep: Reputation: 15
Code:
class numbers
{
	public static void main(String args[])
	{
		int numbers[][] = new int[3][];
		numbers[0][] = {1,2,3,4};
		numbers[1][] = {5,6};
		numbers[2][] = {7,8,9,10,11,12};
		
	}
}
that's the new code, still doesn't work though

also doesn't work when i use
Code:
		numbers[0] = {1,2,3,4};
		numbers[1] = {5,6};
		numbers[2] = {7,8,9,10,11,12};
which i think i'm supposed to do

either way, i think it might not be possible to assign numbers[0] to a set of values, then to assign numbers[1] on the next line or later in the program. I think that they all need to be done at once. Can somebody confirm this?
 
Old 08-09-2007, 12:52 PM   #4
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 43
http://java.sun.com/docs/books/jls/s...rrays.doc.html
 
Old 08-09-2007, 01:34 PM   #5
bijit1709
Member
 
Registered: Oct 2006
Location: Costa Rica, Escazu
Distribution: Slackware 12.0 (2.6.21.5)
Posts: 67

Rep: Reputation: 15
In order to declare a valid array you need something like this:

Code:
int array[][] = new int [int][int];
You need to give the "new int [][]" parameters you can not leave it blank. By either entering manually or asking for it?

eg;

Code:
int array[][] = new int [2][3];
or

Code:
size1 = Interger.ParseInt(JOptionPane.showInputDialog("Enter the first size of the array."));
size2 = Interger.ParseInt(JOptionPane.showInputDialog("Enter the second size of the array."));
int array[][] =  new int [size1][size2];
And then Full it out with what ever numbers you want.
 
Old 08-10-2007, 07:15 AM   #6
elsheikhmh
Member
 
Registered: Aug 2004
Location: Cairo, Egypt
Distribution: Slackware
Posts: 101

Rep: Reputation: 15
You cannot assign numbers[0] = {1, 2, 3, 4}. Instead, each row is a one dimensional array by itself.
Code:
public class Numbers
{
	public static void main(String args[])
	{
		int numbers[][] = new int[3][];
		numbers[0] = new int[] {1,2,3,4};
		numbers[1] = new int[] {5,6};
		numbers[2] = new int[] {7,8,9,10,11,12};
	}
}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
error in creating array bhandu Linux - Newbie 8 07-12-2007 12:18 AM
How to allocate memory for a multidimensional array ? indian Programming 12 11-19-2004 04:37 AM
return index of element in java multidimensional array dave bean Programming 8 11-28-2003 12:00 PM
multidimensional array (php) niehls Programming 2 01-31-2003 06:34 AM
PHP: Multidimensional array problem lhoff Programming 1 06-10-2002 03:49 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:20 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration