-1

I want to change an element name in an ArrayList, without changing its value. Like :-

ArrayList<Integer> array1 = new ArrayList<>();
int num = 1;
array1.add(num);

Now I want to change num to number, but it's value will be 1. (Is it possible?)

I want to display all the elements in a ArrayList in a swing combo box, but some of the variables show strange names, such as "Client@45673...". But I want the user to see a descriptive name. That is why I want to rename the variables.

  • 3
    You have a misconception here. Things in an array list don't have names. This seems like a [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). What are you actually trying to do? – Sweeper Nov 28 '20 at 03:32
  • 1
    What do you want to achieve out of it ? Please explain . – Ajeet Maurya Nov 28 '20 at 03:38
  • I have updated the question. Now take a look at it. – UnknownCoder 56 Nov 28 '20 at 04:13
  • The question you're asking seems unrelated to the problem you're having. The code as shown doesn't _"show strange names, such as "Client@45673...""_. That problem sounds like [How do I print my Java object without getting “SomeType@2f92e0f4”?](https://stackoverflow.com/questions/29140402/how-do-i-print-my-java-object-without-getting-sometype2f92e0f4) – Mark Rotteveel Nov 28 '20 at 10:17

2 Answers2

1

OK, so you are wandering up the proverbial garden path with your "change the name of a variable".

Firstly, it is not a variable. When you put something into a collection, Java doesn't remember that at one point you had the value in a variable.

Secondly, values in general and objects in particular don't have names ... unless you specifically implemented some kind of "name" field as part the class definition.

But I think the clue is in this sentence:

... but some of the variables show strange names, such as "Client@45673..."

That's not a name! That looks like the output of the default implementation1 of toString that your Client class is inheriting from java.lang.Object.

If you want to print / display something something more meaningful, your Client class needs to override the toString() method with its own implementation.

For example:

public class Client {
    private String myDescription;

    public Client(...) {
        ...
        myDescription = /* something */
    }

    @Override
    public String toString() {
        return myDescription;
    }
}

Your toString method can return anything you want it to ... so long as it is represented as a string.

Incidentally, if you were to print the array1 object in your question, you would see the actual value 1 rather than a "name" (as you called it). This is because java.lang.Integer overrides the toString() method.


1 - The default toString() result consists of the internal class name and the object's identity hashcode value. The latter is a magic number that the JVM generates. It is not guaranteed unique, but it is guaranteed that it won't change for the lifetime of the object. At any rate, you cannot alter the string that is generated by that method ... except by overriding it.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

What you're talking about is the output when printing an object. Those objects need to override toString() and return whatever string is deemed appropriate that describes the class or the contents thereof.

Try this.

class Foo {
    //@Override
    //public String toString() {
    //  return "This is a foo";
    //}
}

Foo f = new Foo();
System.out.println(f);

Now uncomment the toString() method and print it again.

To print arrays, one way is to use Arrays.toString()

int[] arr = {1,2,3,4};
System.out.println(Arrays.toString(arr));
WJS
  • 36,363
  • 4
  • 24
  • 39