Array Objects

Hi i'm having a problem with the following:

Object array1 ={"Name", 6, 5}

int number = array1.getInt(array, 1);

What I am trying to do is to have an array which has a mixture of primitive types, and then put an int from a certain index into an int variable so I can use it in maths.

The error i'm getting is "cannot find symbol". But the method "getInt" is in lang so I don't know what the problem is...

I'm sure there's something stupid I'm doing so if someone could point it out i'd appriciate it.

[655 byte] By [Don88a] at [2007-11-26 16:29:24]
# 1

First of all, primitive types are not objects. int, float, boolean do not extend class Object because they are not classes. To add primitive types, use wrapper classes such as Integer and Boolean.

Second of all, an array is declared with []. It's Object[] array1=new Object[] {elements}.

Third of all, there is no getInt() method in arrays.

Lord_Jirachia at 2007-7-8 22:53:48 > top of Java-index,Java Essentials,New To Java...
# 2

> What I am trying to do is to have an array which has

> a mixture of primitive types,

You can't. An array cannot hold both primitives and objects. If you put a primitive into an Object array, it will be autoboxed. You shouldn't mix types in an array anyway. Define a class instead.

> The error i'm getting is "cannot find symbol". But

> the method "getInt" is in lang so I don't know what

> the problem is...

Object does not have a getInt method. Arrays don't have a getInt method. Just because that method exists somewhere in the API, that doesn't mean you can just call it on any object.

You should go through a Java tutorial from the beginning, to learn about classes and methods.

[url=http://java.sun.com/docs/books/tutorial/]Sun's basic Java tutorial[/url]

[url=http://java.sun.com/learning/new2java/index.html]Sun's New To Java Center[/url]. Includes an overview of what Java is, instructions for setting up Java, an intro to programming (that includes links to the above tutorial or to parts of it), quizzes, a list of resources, and info on certification and courses.

[url=http://javaalmanac.com]http://javaalmanac.com[/url]. A couple dozen code examples that supplement [url=http://www.amazon.com/exec/obidos/tg/detail/-/0201752808?v=glance]The Java Developers Almanac[/url].

[url=http://www.jguru.com]jGuru[/url]. A general Java resource site. Includes FAQs, forums, courses, more.

[url=http://www.javaranch.com]JavaRanch[/url]. To quote the tagline on their homepage: "a friendly place for Java greenhorns." FAQs, forums (moderated, I believe), sample code, all kinds of goodies for newbies. From what I've heard, they live up to the "friendly" claim.

Bruce Eckel's [url=http://mindview.net/Books/DownloadSites]Thinking in Java[/url] (Available online.)

Joshua Bloch's [url=http://www.amazon.co.uk/exec/obidos/Author=Bloch,%20Josh]Effective Java[/url]

Bert Bates and Kathy Sierra's [url=http://www.amazon.com/exec/obidos/tg/detail/-/0596004656?v=glance]Head First Java[/url].

James Gosling's [url=http://www.bookpool.com/sm/0321349806]The Java Programming Language[/url]. Gosling is

the creator of Java. It doesn't get much more authoratative than this.

jverda at 2007-7-8 22:53:48 > top of Java-index,Java Essentials,New To Java...
# 3
Ok How is a wrapper used?
Don88a at 2007-7-8 22:53:48 > top of Java-index,Java Essentials,New To Java...
# 4
> How is a wrapper used?You ignored all the other advice and just focussed on how to hack your way to a solution, didn't you?
floundera at 2007-7-8 22:53:48 > top of Java-index,Java Essentials,New To Java...
# 5

Object array1 = { "Name", new Integer(6), new Integer(5)};

It will work but it is dirty hack. I would never do it in my programm.

And they are not primitive of cause.

_Dima_a at 2007-7-8 22:53:48 > top of Java-index,Java Essentials,New To Java...
# 6
If it's only going to hold primitives, then why not make an int array?int array = {1, 2, 7, 4, 9};int a = array[0]; // = 1
CaptainMorgan08a at 2007-7-8 22:53:48 > top of Java-index,Java Essentials,New To Java...
# 7
> First of all, primitive types are not objects. int,> float, boolean do not extend class Object because> they are not classes. To add primitive types, use> wrapper classes such as Integer and Boolean.Read up on AutoBoxing. It's new to 1.5.
CaptainMorgan08a at 2007-7-8 22:53:48 > top of Java-index,Java Essentials,New To Java...
# 8

Attached sample of autoboxing and unboxing using JDK 1.5

public class ArrayTest

{

public static void main(String args[])

{

Object[] array = {"Name", 1, 5.0};

String name = (String) array[0];

int number = (Integer) array[1];

double dbl = (Double) array[2];

}

}

paternostroa at 2007-7-8 22:53:48 > top of Java-index,Java Essentials,New To Java...
# 9
> Read up on AutoBoxing. It's new to 1.5.Sorry, I'm only familiar with 1.3.
Lord_Jirachia at 2007-7-8 22:53:48 > top of Java-index,Java Essentials,New To Java...
# 10

I have a question concerning array objects.

I have three classes.

the main, the classe which contains a method that creates an arrayList from the keyboard input and another class that should calculate the arrayList components and print out a result.

from the main i call the new class().

this class contains several methods that add values to an arrayList created in that particular class.

I want to take this arrayList and send it back to the main so the main can ship it back to the third class.

I have tried several things with no success.

My problem is that when the array is created and i go back in the main, i can't retrieve the data and if i try to go back in the class that had the arrayList this arrayList will be reinitialized.

so i would like to be shown the proper way to do it. thank You

wclefa at 2007-7-8 22:53:48 > top of Java-index,Java Essentials,New To Java...