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.

Results 1 to 3 of 3

Thread: Is there a way to make an array with different variables

  1. #1
    Junior Member
    Join Date
    Apr 2021
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Is there a way to make an array with different variables

    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

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,079
    Thanks
    53
    Thanked 2,616 Times in 2,566 Posts

    Default Re: Is there a way to make an array with different variables

    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.

  3. #3
    Junior Member
    Join Date
    Apr 2021
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Is there a way to make an array with different variables

    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());
    		}
    	}
    }

Similar Threads

  1. How do you make a sum of variables in a loop?
    By Al Rayne in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 13th, 2014, 05:49 AM
  2. Searching for object variables inside a 1-D array?
    By Scorks in forum Collections and Generics
    Replies: 2
    Last Post: September 6th, 2013, 11:36 PM
  3. Why do we make variables of a class private?
    By Pratyush in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 1st, 2013, 02:26 AM
  4. Replies: 4
    Last Post: December 19th, 2011, 09:57 PM
  5. Changing Array variables from different classes
    By smellyhole85 in forum Collections and Generics
    Replies: 6
    Last Post: December 9th, 2010, 03:18 PM

Tags for this Thread