I want to create an array like the list ones like the ones in python3 in java but I don't know how to do it Ex: {"John", 1, True, 1.7}
Is it possible to create an array of more than one datatype in java If it is possible, how is it done?
I want to create an array like the list ones like the ones in python3 in java but I don't know how to do it Ex: {"John", 1, True, 1.7}
Is it possible to create an array of more than one datatype in java If it is possible, how is it done?
Is it possible to create an array of more than one datatype in java If it is possible
No, not possible. But you're mistaken - in python, this is also impossible.
The trick is, in Python all objects are just 'object', and you have a list of objects. In java, expressions do have a type. Nevertheless, all objects are, well, objects. So:
Object[] o = {"John", 1, true, 1.7};
works. You really don't want this - arrays are low level constructs. You'd want List<Object> o = List.of("John", 1, true, 1.7}; no doubt.
Also, why do you want to store this in a list? It SOUNDS like you want this:
class Person {
String name;
int id;
boolean enrolled;
double gpa;
}
and then a List<Person>. That is 'the java way'. Junking that stuff in an Object[] is not the java way. When in rome, act like romans. When coding java, write it like java people would. If you do not, anybody else can't read your code, and libraries do not work for you or feel weird and unwieldy. If you insist on programming 'python style', then just use python.
Use records in Java 16 and later.
record Person ( String name , int talentCode , boolean isAvailable , double grading ) {}
Given your example of {"John", 1, True, 1.7}, I suspect you have a set of properties related to a single entity rather than a collection of siblings. In other words, you have tuple.
If this is case, such as all four of those values describing a single person, then in Java we would create a class named Person to hold four member fields. Each field would be of the appropriate type.
package work.basil.example.recs;
import java.util.Objects;
public final class Person
{
private final String name;
private final int talentCode;
private final boolean isAvailable;
private final double grading;
public Person ( String name , int talentCode , boolean isAvailable , double grading )
{
this.name = name;
this.talentCode = talentCode;
this.isAvailable = isAvailable;
this.grading = grading;
}
public String name ( ) { return name; }
public int talentCode ( ) { return talentCode; }
public boolean isAvailable ( ) { return isAvailable; }
public double grading ( ) { return grading; }
@Override
public boolean equals ( Object obj )
{
if ( obj == this ) return true;
if ( obj == null || obj.getClass() != this.getClass() ) return false;
var that = ( Person ) obj;
return Objects.equals( this.name , that.name ) &&
this.talentCode == that.talentCode &&
this.isAvailable == that.isAvailable &&
Double.doubleToLongBits( this.grading ) == Double.doubleToLongBits( that.grading );
}
@Override
public int hashCode ( )
{
return Objects.hash( name , talentCode , isAvailable , grading );
}
@Override
public String toString ( )
{
return "Person[" +
"name=" + name + ", " +
"talentCode=" + talentCode + ", " +
"isAvailable=" + isAvailable + ", " +
"grading=" + grading + ']';
}
}
Using this class.
Person alice = new Person( "Alice" , 1 , true , 1.7 ) ;
System.out.println( alice.getName() ) ;
Alice
recordWriting such a class is much easier in Java 16, if the main purpose of such a class is to transparently and immutably hold this data.
The new records feature lets us define a class briefly, declaring the member fields only. The compiler implicitly creates the constructor, getters, equals & hashCode, and toString.
Records can be thought of as nominal tuples, to quote JEP 395. The “nominal” means each value is named, and “tuple” means a finite ordered sequence of values.
public record Person ( String name , int talentCode , boolean isAvailable , double grading )
{
}
Using this record.
Person alice = new Person( "Alice" , 1 , true , 1.7 ) ;
System.out.println( alice.name() ) ;
Alice
A record can be written as a separate class, nested within another class, and can even be defined locally within a method.