How to judge isInstance(ArrayList<>) in java [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
How to judge whether a variable is an ArrayList in Java? Like the isInstance function.

Just use instanceof, blow is very simple example how to detect a variable is ArrayList:
List<String> strings = new ArrayList<>();
if (strings instanceof ArrayList) {
System.out.println("ArrayList");
}

a.getClass().getName() - will give you the type of the value of the variable, but not the variable's type.
boolean b = a instanceof String - will give you whether or not it is an instance of a specificclass.
I took this information from: How know a variable type in java?
This can happen. I'm trying to parse a String into an int and I'd like to know if my Integer.parseInt(s.substring(a, b) is kicking out an int or garbage before I try to sum it up.

Related

Use the value that a function returns as the name of a new Array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Good I am trying to create a function that generates new Array, and trying to use a getter method that returns a name for example ArrayD5, ArrayD10 and use these as the name of the new arrays to generate.
I have tried to do this:
int length = 5;
String (SeatIbiza.nombres(length))[] = new String[length];
String nombres (int length) {
return "ArrayD"+length;
}
If you are looking for a way to declare something like e.g.
String "ArrayD" + 2
so that you have a variable
String ArrayD2
then this is not possible in Java. Dynamic naming of variables is not supported.
I don't think if that's possible. The variable names are required to be declared during compile time in JAVA and dynamic naming isn't supported... yet.
Correct me if I'm wrong as I'm still learning about JAVA

Java: Sum of values inside a Collection of DTO [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Calculate the sum of amountField in MyDTO, Java 8
private Double getSum(List<MyDTO> myDTOList) {
return myDTOList.stream().map(MyDTO::getAmount).reduce(0d, Double::sum);
}
You can use Stream.mapToDouble to convert it into a DoubleStream and then use DoubleStream.sum:
return myDTOList.stream().mapToDouble(MyDTO::getAmount).sum();
To mention, the above shall work for amount being of integer type as well, while the code shared by you and this shall still work fine if the type of amount is already double.
Or as pointed out in comments by you, if your DTO object can be null, you can filter out those values using:
return myDTOList.stream().filter(Objects::nonNull).mapToDouble(MyDTO::getAmount).sum();

Is there any way to auto generate objects from a class? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have a class with 5 variables, and 3 out of the 5 will be the same for every new object. Is there a way to create, say, 100 objects, from a string file containing 100 items?
Yeah of course can you do that. I wont write code for you, but the following steps should help to come to a solution yourself.
Make a constructor delegation, one which accepts 5 values. And another which accepts just the 2 which are not changing. This constructor then overloads the other with the default values. this(3,2,"a", input1, input2)
Initialize the 3 variables which are not changing directly in the field declaration: private int initialSetVariable1 = 3;

Create variable execution time Java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Is possible to do in Java something like this? :
System.out.... -> ask for a string
Store this string
Create a variable with the name of this string
Thank you very much!
No. You cannot create a variable, at execution time, with a particular name.
However, you can use a Map, which probably does what you want. The get method will look up an entry, and the put method will set an entry. Example:
Map<String, Integer> myMapOfThings = new HashMap<>();
String nameOfThing = /* ... get a string from the user somehow here ... */;
myMapOfThings.put(nameOfThing, 2);
System.out.println(myMapOfThings.get(nameOfThing)); // prints 2

Java mismatch between String and String[] [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I'm using Java to implement the Apriori algorithm but one problem exists.
ArrayList<String> l1=new ArrayList<>();//L1
...
ArrayList<String[]> lk1=l1;//Lk-1
Then it warns that: cannot convert from ArrayList<String> to ArrayList<String[]>. How can I solve this problem?
They're different types. Ignore the collection and just look at the underlying type.
String str = "This is a string.";
String[] array = str;
These are two different types, unconvertable, so as T for the collection, the collections are then unconvertable. Very basic concept here.

Categories

Resources