• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

anonumous arrays?

 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can this be created ?
i tried the following
new int[]{1,2,3};
the compiler shouted ?
plz explain??
 
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
IMHO,either you use new int[]; on the right side
or even new int{1,2,3};
But both of them at the same time will not work.
Hope i made a point.
regds
NM
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by sunilkumar ssuparasmul:
can this be created ?
Try the following
int arr[] = new int[]{1,2,3};

System.out.println("arr[0]" + arr[0]); //prints 1

Regards
Arvind


 
sunilkumar ssuparasmul
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello guys,
i was talking abt anonymous arrays yaar. How do u create an array without giving it a name?
Bill ,jane and others please clarify ?
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am unable to create like what you have done However I was able to create one and return it from a function
I am giving an example below which creates anonymous arrays of primitive data types and anonymous arrays of objects and returns from methods;
//file: test.java
public class test
{
int num[];
numbers getnumber[];
public static void main(String []aa)
{
test testing = new test();
numbers someNumber = new numbers();
testing.num = testing.getint();
testing.getnumber = someNumber.getnumbers();
for ( int i = 0; i < testing.num.length; i++)
System.out.println(testing.num[i]);
}
public int[] getint()
{
return new int[] { 1,2,3,4};
}
}

class numbers
{
int val;
static int inst;
numbers()
{
val = inst++;
System.out.println( "Value : " + val);
}
public numbers[] getnumbers()
{
return new numbers[] { new numbers(), new numbers(), null, new numbers() };
}
}
HTH
Sasidhar
 
sasi dhulipala
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Further I am able to pass anonymous arrays of both primitive as well as object type to methods..
Sasidhar
 
sunilkumar ssuparasmul
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hai Sasi,
But here agian arent u not giving names to bothe the arrays like testing.num and testing.getnumber . i just wanted to know can u create and use anonymous araays like listener interfaces.
or in GENAERAL can anonymous arrays be created in java - true or false
 
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What i found is anonymous arrays cannot be created just hanging freely in a code. But it can be printed while creation.
Yes. Anonymous arrays are allowed in java.
class anonyarrays{
public static void main(String[] args){

//new int[] {3,4,5}; // anonymous arrays cannot be created hanging in a program.
System.out.println(new int[] {3,4,5});

}

}

HTH
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi sunilkumar,
The JLS describes an array as both a type and an Object ... it implicitly extends java.lang.Object but it cannot be extended; just as you cannot extend <code>int, boolean, float</code> etc. In a sense, I think an array is, itself, an anonymous class that Java implements as part of the language.
For example, when you create an Anonymous class:
<code>new ClassName() { // some methods } </code>
you are telling the compiler you want a new object based on ClassName. Once you extend the class using an anonymous class, every time you create another object of the original class (within the same code), the anonymous class definition is used, not the original. ie the original class name acts as a reference to the new type.
When you create an array:
<code>new int [] { 1,2,3 }</code>
you are telling the compiler you want a new object of array type that will contain components of type int.
The square brackets [] act the same as a class name in an Anonymous class creation.
So, in a sense, every time you create an array, you are creating an anonymous class that cannot be extended as it's a built in type and has the limitation of requiring a variable name to access it as you can't use the class name directly.
Hope that helps.


------------------
Jane Griscti
Sun Certified Java 2 Programmer
 
natarajan meghanathan
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jane,
Good explanation. But my question is does the compiler expects the anonymous array to be assigned to any reference of the same type?. Because as i showed the code, i could not create it just as a free bird. But i could print it while creation. Can you pour some thoughts to this scenario?
TIA
Natarajan
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic