1

I am learning Java and I am doing some C++ codes into java, I am following this webpage http://uva.onlinejudge.org and trying to do some of the problems in Java. Now I am doing this problem http://uva.onlinejudge.org/index.phpoption=com_onlinejudge&Itemid=8&page=show_problem&problem=1072 and after researching and figure out how to do it on paper I found this webpage where the problems seems easy to follow: http://tausiq.wordpress.com/2010/04/26/uva-10131/

But now, and due to the fact I am new on Java, I want to learn how to do an array of struct in Java. I now I can do a class like a struct: if this is in C++

struct elephant {
    int weight;
    int iq;
    int index;
} a [1000 + 10];

I can do this in Java:

public class Elephant {
        private int _weight;
        private int _iq;
        private int _index;

        public Elephant(int weight, int iq, int index) {
            this._weight = weight;
            this._iq = iq;
            this._index = index;
        }

        public int getWeight() {
            return this._weight;
        }

        public int getIQ() {
            return this._iq;
        }

        public int getIndex() {
            return this._index;
        }

        public void setWeigth(int w) {
            this._weight = w;
        }

        public void setIQ(int iq) {
            this._iq = iq;
        }

        public void setIndex(int i) {
            this._iq = i;
        }
    }

But I don't know how I can turn this into the last part of the struct in c++:

a [1000 + 10];

I mean, having an array of a objects of the class Elephant in Java like having an array of elements elephants in c++

Could someone help me to understand it better..

1
  • Is there any point declaring the data members private in Java? They are public in your C++ example after all. Commented May 26, 2014 at 15:22

2 Answers 2

5

Arrays of objects in Java are accomplished the same way as an array of primitives. The syntax for this would be

Elephant[] elephants = new Elephant[1000+10];

This will initialize the array, but it will not initialize the elements. Any index into the array will return null until you do something like:

elephants[0] = new Elephant();
Sign up to request clarification or add additional context in comments.

3 Comments

Indeed, i was going to ask if i could do this: ArrayList<Elephant> elephant = new ArrayList<Elephant>(); thank you
@neteot you can! Although that's an ArrayList which is closer to an STL list or more generically a linked list. The ArrayList is another object that is backed by an array and allows you to dynamically adjust size etc. Arrays, once created, are a fixed size.
It will initialize the elements: with null, and that's a guaranteed behaviour. (This post is for C++-devs learning Java, so I think this pedantry is appropriate here, since C++ may do weirder things for an uninitialized array element)
1

This should be what you are loking for :

Elephant []  array = new Elephant[1010];

2 Comments

Indeed, i was going to ask if i could do this: ArrayList<Elephant> elephant = new ArrayList<Elephant>(); thank you
@netot Yes you can do so.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.