1
public class ReferenceTest {
    public static void main(String[] args) {

        String[][] names = {{"George","Hampton"},{"Marc", "Empten"},{"Levin", "Lian"}};

        Object oneperson; //Reference to one object of the 2D Array

        oneperson = names[1]; 

        //Output should be Marc Empten
        System.out.println(oneperson.toString()); //[Ljava.lang.String;@11890d

    }
}

Is it possible to create such a reference in java? So I can save an array-element(

{"Marc","Empten"}

) from the array?

So I can use only the oneperson variable to give out the "Marc Empten"? I have no Idea how to realise this. Is it even possible?

0

3 Answers 3

2

If you want to just print an array use Arrays.toString() method. But the better approach her would be to make a class Name which hold both parts. Then use an array list to store the names. Like

class Name{
    private String firstName;
    private String lastName;

    public Name(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    @Override
    public String toString() {
        return "{" + firstName + ", " + lastName
                + "}";
    }
    // Getters & Setters
}

public class Sample {

    public static void main(String[] args) throws JsonProcessingException {
        List<Name> names = new ArrayList<Name>(Arrays.asList(new Name[] { new Name("George","Hampton"), new Name("Marc", "Empten"), new Name("Levin", "Lian")}));
        System.out.println(names.get(1)); // Prints {Marc, Empten}

    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

so i must always use names.get(number) i wanna use a "object" like feuerball it did
This is almost the same as names[number]. Lists have the benefit that you have no constraints in the size.
List is basically an array underneath the covers. So its similar to feuerball number except it can grow as an when you add items. I didnt full understand what you meant by "i wanna use a object". Did you mean you need to do this as an array?
no i want to have a reference object like evry other variable for explizit one element of an array or arraylist...
Yes. That what the indexing does. It returns the element at index 1. Which is equivalent to Name oneperson = names.get(1); or you could like Object oneperson = names.get(1);.
1

Do it like this:

System.out.println(Arrays.toString((Object[])oneperson));

4 Comments

This does not work because oneperson is an Object and not an array.
Additionally it would print [Marc, Empten] if it was working.
with a cast this is possible :
Added Cast to an array
1

You would have to write something like this:

public class ReferenceTest {
    public static void main(String[] args) {
        // An array of arrays of Strings
        String[][] names = {{"George","Hampton"},{"Marc", "Empten"},{"Levin", "Lian"}};
        // An array of Strings
        String[] marc = names[1]; 
        //Output is Marc Empten
        System.out.println(marc[0] + " " + marc[1]);
    }
}

This only works if you know for sure that the inner arrays always contain 2 elements. If not you would have to use a loop to print the name for example (note that the last name has 3 parts):

final String[][] names =
    { { "George", "Hampton" }, { "Marc", "Empten" }, { "Levin", "Lian" }, { "John", "James", "Rambo" } };

for (final String[] name : names) {
    for (final String partial : name) {
        System.out.print(partial);
        System.out.print(" ");
    }

    System.out.println();
}

Comments

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.