I want to make an array but i don't want only words or only numbers, i want to have multiple variables, there might be a better way of doing this, but i really dont know what im doing
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
I want to make an array but i don't want only words or only numbers, i want to have multiple variables, there might be a better way of doing this, but i really dont know what im doing
The contents of an array must be all of the same type; for example all int or all String.
Since all classes extend the Object class, an array of Object would be able to hold references to any class, but NOT any primitives like int or char.
Why do you want to have mixed types in an array?
If you don't understand my answer, don't ignore it, ask a question.
Simply, wrap the data in a class like this:
class Mix { private int number; private String word; public Mix(int number, String word){ this.number = number; this.word = word; } public void setNumber(int number) { this.number = number; } public int getNumber() { return this.number; } public void setWord(String word) { this.word = word; } public String getWord() { return this.word; } } class Main { public static void main(String[] args) { Mix[] mixArray = { new Mix(1, "X"), new Mix(2, "Y"), new Mix(3, "Z") }; for(Mix mix : mixArray) { System.out.println("number=" + mix.getNumber() + ",word=" + mix.getWord()); } } }