This code block prints "not contains" and i don't know why, can somebody help me ?
private static final int[] YEARS = new int[] {
1881, 1893, 1900, 1910, 1919, 1923, 1930, 1932, 1934, 1938
};
public static void main(String[] args) {
int year = 1919;
System.out.println(YEARS);
if (Arrays.asList(YEARS).contains(year))
{
System.out.println("Contains");
}
else {
System.out.println("Not contains");
}
// Write your code here below
}
}
Arrays.asList(YEARS) will result in a List<int[]>, you will not find a single int in there using the contains(int) method of a List…
You probably expected a List<Integer>, which you can get by
List<Integer> years = Arrays.stream(YEARS).boxed().collect(Collectors.toList());
The resulting List<Integer> can be checked for the presence of a single int using the contains method because it is a list of integers contrary to the list of arrays of integers produced by Arrays.asList(YEARS).
that's because you are using int
in your arraylist when you should be using Integer
int is a primitive type and doesn't work with Arraylist which requires a non-primitive type
change int to Integer and that code should work
Related
I would like following effect -> I have object of class FluidArray which will be an array, but depending on the input it will be either int array or String array:
FluidArray XXX = new FluidArray;
XXX.YYY[] might be either String or int
In this case variable YYY of class XXX might be int array or String
Can I somehow declare variable type depending on some choice?
public class FluidArray
{
VarType YYY;
public static void FluidArray(int a)
{
double[] YYY = new double[15];
}
public static void FluidArray(String a)
{
String[] YYY = new String[15];
}
}
Let's say I want to make a sort method.
I input there unsorted array.
I take out sorted array.
The catch is I might want to sort String, double or int array and I don't want to write 3 sorting methods - I thought that my sorting method might work on some defined object and this object will be either String, double int depending on my choice.
I am trying to use Generic type, I got so far sth. like this:
public class test
{
public static void main(String[] args)
{
FluidArray<Integer> arrTest = new FluidArray<>();
arrTest.arr[1]=2;
arrTest.arr[2]=3;
arrTest.arr[3]=4;
}
public static class FluidArray<arrType>
{
public arrType[] arr = (arrType[])new Object[15];
}
}
I don't understand, why I can't get access to the array, compiler ends when inserting first value.
Read up on Generics. Thats what they are supposed to do
This question already has answers here:
java generics: can't create a simple print array method
(2 answers)
Closed 2 years ago.
I have started learing java generics and this is my priority queue implementation:
public class MinPQ<Key> implements Iterable<Key> {
private Key[] pq; // store items at indices 1 to n
private int n; // number of items on priority queue
private Comparator<Key> comparator; // optional comparator
public MinPQ(Key[] keys) {
n = keys.length;
pq = (Key[]) new Object[keys.length + 1];
.....
.....
}
}
And this is my main class :
public class Demo {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] ar = {1,2,3};
MinPQ<Integer> pq = new MinPQ<Integer>(ar);
}
}
But here I get an error stating "The constructor MinPQ(int[]) is undefined" can someone please help me find the issue here ?
You can't use primitive types with generics. Just change:
int[] ar = {1,2,3};
to:
Integer[] ar = {1,2,3};
or box it with
IntStream.of(ar).boxed().toArray(Integer[]::new)
Basically you're trying to pass an int[] to a constructor which requires Integer[], because of MinPQ<Integer> declaration. So, in tehory it should work if you declared it as MinPQ<int> - but as stated at the beginning we can't use primitive types for generics.
This question already has answers here:
What's the simplest way to print a Java array?
(37 answers)
Closed 7 years ago.
I'm new to Java and is trying to learn the concept of constructor. I tried to print out the value of arrayOfInts in the main method using (to test whether the constructor was initialised the way I expected)
System.out.println(ds.arrayOfInts);
However, instead of printing out the values, the output is:
[I#15db9742
Why am I getting the wrong result and how I can print out the correct result? (i.e. the value stored in arrayOfInts).
public class DataStructure {
public static void main(String[] args) {
DataStructure ds = new DataStructure();
//System.out.println(ds.arrayOfInts); Doesnt work as expected
}
private final static int SIZE = 15;
private int[] arrayOfInts = new int[SIZE];
public DataStructure() {
int arrayValue = 0;
for (int i = 0; i < SIZE; i++) {
arrayOfInts[i] = ++arrayValue;
}
}
}
You are trying to print an array. An array is an object.
In order to display it correctly, you can loop through it, or use the Arrays.toString() method:
System.out.println(Arrays.toString(ds.arrayOfInts));
which returns a string representation of the specified array.
Arrays are objects in java. You need to iterate over elements and print them. Here is a sample implementation using the for-each loop.
public void print() {
for (int x : arrayOfInts) {
System.out.println(x);
}
}
new to Java. Trying to understand the point of declaring my ArrayList as an <Integer>. I still have to cast my .get() result as an int in my methods for it to work, else it still returns an Object. eg: (int) deliv.get(j) int the Sort method's for loop. Is there a way to avoid this or is my code the correct approach?
Problem: Assume the array can change size, hence not just using primitive array. All numbers should be pairs, looking for the unique one missing it's pair value. I sort the array, then cycle through the pairs to look for a mismatch. Thanks.
import java.util.*;
class StolenDrone{
public static void main(String[] args){
ArrayList<Integer> deliv_id = new ArrayList<>();
deliv_id.add(99);
deliv_id.add(13);
deliv_id.add(4);
deliv_id.add(5);
deliv_id.add(8);
deliv_id.add(99);
deliv_id.add(8);
deliv_id.add(5);
deliv_id.add(4);
System.out.println("Array is: " + deliv_id);
sort(deliv_id);
System.out.println("Array is: " + deliv_id);
int unique = findUnique(deliv_id);
System.out.println("Unique ID is: " + unique);
}
//Sort ArrayList into increasing order
static void sort(ArrayList deliv){
int temp;
for(int i = 0; i<deliv.size();i++){
for (int j=0; j<deliv.size()-1;j++){
if((int) deliv.get(j)> (int) deliv.get(j+1)){
temp = (int) deliv.get(j+1);
deliv.set(j+1, deliv.get(j));
deliv.set(j, temp);
}
}
}
}
//check pairs in ArrayList to find unique entry
static int findUnique(ArrayList deliv){
for(int i = 0; i<deliv.size()-1;i+=2){
if(deliv.get(i) == null){
return -1; //no unique
}
if((int) deliv.get(i) != (int) deliv.get(i+1)){
return (int) deliv.get(i);
}
}
return -1;
}
}
When you type parameterize ArrayList<Integer> the compiler knows that everything inside the ArrayList is of type Integer and will only allow you to add Integers to the list, and thus get() returns Integer. Without parameterizing the compiler will allow you to add any Object to the ArrayList, and thus calling get() will return an Object and requiring the cast to int.
To remove the need for casts you need to change parameters with type ArrayList to ArrayList<Integer> in the function declaration.
static void sort(ArrayList deliv)
Your method signature requests an untyped ArrayList.
The compiler cannot know what will be inside the ArrayList so it requires you to cast the result.
Change it to this:
static void sort(ArrayList<Integer> deliv)
Now the compiler knows it is an ArrayList of Integers.
So you wont need to add the cast to get()
In Java Integet is wrapper-class of int. You cannot set int as a type of ArrayList to work, but you can put there int type and it will be automatticly casted to Integer. To meke work it good you should do like this :
static void sort(ArrayList deliv){
int temp;
for(int i = 0; i<deliv.size();i++){
for (int j=0; j<deliv.size()-1;j++){
if(deliv.get(j)> deliv.get(j+1)){ // You should not cast, Integer is Comparable
temp = deliv.get(j+1).intValue();//Changes here
deliv.set(j+1, deliv.get(j).intValue());//And here
deliv.set(j, temp);
}
}
}
}
Good luck
public static void main(String args[])
{
double arr[] = {1,-6.3,9000,67.009,1.1,0.0,-456,6,23,-451.88};
ArrayList<Integer> List = new ArrayList<Integer>();
List.add(1);
List.add((int) -6.3);
List.add(9000);
List.add((int) 67.009);
List.add((int)1.1);
List.add((int)0.0);
List.add(-456);
List.add(6);
List.add(23);
List.add((int)451.88);
}
public static int ArrayListMax(ArrayList List)
{
for (int i=0; i<List.size(); ++i)
{
System.out.println(List.get(i));
}
The error is in:
public static int ArrayListMax(ArrayList List)
This is probably a very nooby mistake, but I'm new to Java so forgive me.
Any help please?
Thank you.
EDIT:
I want the ArrayListMax method to print the size of the List!
Assuming you are trying to get the maximum value in your List in the method arrayListMax, you need to return an integer in accordance with your method signature, which the error is telling you
This method must return a result of type int
Instead of printing all the values in the list, you could do:
public static int arrayListMax(List<Integer> List) {
return Collections.max(list);
}
Use Java Naming Conventions. Method & variable names begin with a lowercase letter. Using this approach helps avoid confusion between instances & types (e.g. in the case of List in the main method).
Either add a return statement to your ArrayListMax() method that returns an int or change the method signature from public static int to public static void. And add a closing } to the method too.
Also, you shouldn't use List as a name for the argument to that method because it's the name of an interface that you're actually importing in this code. The convention in java is for variable names and method names to begin with a lowercase letter (camel case) and class names to begin with an uppercase letter (pascal case).
Problem is in this method:
public static int ArrayListMax(ArrayList List)
{
for (int i=0; i<List.size(); ++i)
{
System.out.println(List.get(i));
}
}
You are not returning from this method, you must return of type int to solve that error. Since you have specified int as return type.
You created the function as returning a result
public static int ArrayListMax(ArrayList List)
In order to remove your problem, if you need not return anything, write
public static void ArrayListMax(ArrayList List)
void means you do not want the method to return a result.
if you are just printing out values the signature should be public static void ArrayListMax(ArrayList List).
First, you are not returning anything with ArrayListMax method even if you have set its retun type to int. Either return void as you are directly printing from the list.
Second, even if you set the correct return type to ArrayListMax method, you will still need to call that method in the main method as below:
public static void main(String args[])
{
double arr[] = {1,-6.3,9000,67.009,1.1,0.0,-456,6,23,-451.88};
ArrayList<Integer> List = new ArrayList<Integer>();
List.add(1);
List.add((int) -6.3);
List.add(9000);
List.add((int) 67.009);
List.add((int)1.1);
List.add((int)0.0);
List.add(-456);
List.add(6);
List.add(23);
List.add((int)451.88);
YourClassNameInWhichMethodIs.ArrayListMax(List);
}
You should use a return statement as Methode is returning an int type value.