The only thing I want to do is putting an array (temp_X) into a HashSet, but I got the error for the HashSet: no suitable constructor found for HashSet(List)
public PSResidualReduction(int Xdisc[][], double[][] pat_cand, int k) {
for (int i = 0; i < Xdisc.length; i++) {
int[] temp_X;
temp_X = new int[Xdisc[0].length];
for (int s = 0; s < Xdisc[0].length; s++) {
temp_X[s] = Xdisc[i][s];
}
HashSet<Integer> temp_XList = new HashSet<Integer>(Arrays.asList(temp_X));
}
}
Any idea how I can fix it?
Arrays#asList accepts a type array which means all elements used need to be Object types rather than primitives.
Use an Integer array instead:
Integer[] temp_X;
This will allow Arrays#asList to be used to against the wrapper class:
HashSet<Integer> temp_XList = new HashSet<Integer>(Arrays.asList(temp_X));
in Arrays.asList(temp_X); temp_X must be a Object array,not primitive type array. And HashSet<T> does not support primitive type .You have to convert each int in temp_X to Integer and add to temp_xList one by one
Related
I have below list defined,
List<BigDecimal> empList = new ArrayList<BigDecimal>();
And as per below code if condition throws error The type of the expression must be an array type but it resolved to List
for(int i1=0;i1<12;i1++) {
if(empList[i1]==null){
empList[i1]= new BigDecimal("0.00");
}
}
The same code works well in Groovy but does not work in Java.
Looks like a syntax error . In java list are accessed as
empList.get(i1)
and
empList.set(i1,new BigDecimal("0.00"))
or you are free to use array instead of List but in this case you will have to define the array length which stays fixed.
BigDecimal[] empList = new BigDecimal[10];
It has to be :
for (int i1 = 0; i1 < 12; i1++) {
if (empList.get(i1) == null) {
empList.set(i1, new BigDecimal("0.00"));
}
}
A list collection needs has its own methods. Use those.
I tried to do:
static boolean newTrait(ArrayList<Integer> population, int[] traits) {
for(int i = 0; i < 25; i++) {
for(int j = 0; j < 625; j++) {
if(traits[j].equals(population[i].get(i))) {
return false;
}
}
}
return true;
}
The java compiler said that I had an "unresolved compiler issue."
Can anyone help?
[X] operator is for arrays. You don't have an ArrayList array you just have an ArrayList<> which, although it has "Array" in the name, is not an array and so cannot be accessed with [X]. ArrayList is an object with regular methods.
It's perfectly valid to have an ArrayList array
e.g.
static boolean newTrait(ArrayList<Integer>[] population, int[] traits)
where each element in the population array IS A ArrayList. In which case you would first need to access the element in the array (via [X]) and then access the elements IN the ArrayList using get()
e.g. get the first element of the array
ArrayList<Integer> firstList = population[0];
then get the Xth item in the list
e.g.
firstList.get(x);
All simple types (not objects) do not have any methods. In your case neither the array not the int values inside the array have methods.
Fix:
if(traits[j] == population.get(i))
{
return false;
}
This compares an int on the left side with an Integer on the right side. For int the == operator is correct but it would be wrong for Integers. However in this case the compiler does automatically 'unbox' the Integer on the right side to int.
Notice that I removed the wrong [i] after population as well. Population is not an array.
I'm trying to create objects within a for loop at runtime. Here is the (incorrect) code:
for(int i=1;i<max;i++){
Object object(i);
}
I'd like it to create max number of Object objects with names object1, object2, etc. Is there any way to do this? I have been unable to find anything elsewhere online. Thanks for your help!
You want to use a data structure to store a sequence of objects. For example, an array could do this:
Fruit banana[] = new Fruit[10];
for (int i = 0; i < 10; i++){
banana[i] = new Fruit();
}
This creates 10 objects of type Fruit in the banana array, I can access them by calling banana[0] through banana[9]
You could use an array to create multiple objects.
public void method(int max) {
Object[] object = new Object[max];
for (int i = 0; i < max; i++) {
object[i] = new Object();
}
}
I'm having an issue understanding some compiler-errors, regarding 2D arrays (ArrayList containing an ArrayList) and generics. My understanding of generics isn't the best, so I tried to research the issue beforehand and still ended up confused.
According to comments on 2D dynamic array using ArrayList in Java, you can't mix arrays with generics (or rather, you can with #SuppressWarnings("unchecked"), but are discouraged from doing so). However, I'm not sure what this exactly means.
Here is my problem code:
blocks = new ArrayList<ArrayList<BarrierBlock>>(columns); // initialize rows
for (int i = 0; i < columns; i++){
// blocks.get(i) = new ArrayList<BarrierBlock>(rows); <- ERROR = (unexpected type; required: variable, found: value)
blocks.add(new ArrayList<BarrierBlock>(rows)); // initialize columns
}
// initilize each block
for (int i = 0; i < blocks.size(); i++){
for (int j = 0; i < blocks.get(i).size(); j++){
int[] blockLoc = {location[0] + (i*BLOCK_SIDE_LENGTH), location[1] + (j*BLOCK_SIDE_LENGTH)};
// blocks.get(i).get(j) = new BarrierBlock(BLOCK_SIDE_LENGTH, blockLoc); <- ERROR = (unexpected type; required: variable, found: value)
blocks.get(i).add( new BarrierBlock(BLOCK_SIDE_LENGTH, blockLoc)); // initialize 2D array elements
}
}
The two lines that I commented out were my initial attempts at initializing the arrays. The compiler complained when I tried this and gave me the error listed. What does this error mean? I would think that both sides are of the declaration statement are variables.
After looking around, I found out that I'm supposed to use the add(E e) method of ArrayList. But what is the main difference? In the new way I'm initializing arrays, doesn't that also "mix arrays with generics"?
Get RETURNS the object at the given index, it can't be used to SET the object.
here are things you CAN do with get:
list l = new list();
item a;
l.add(a);
item b = l.get(0);
b.property = 10;
l.get(0).property == 10; //true, a is the same object as b
b = new item();
l.get(0) == b; //false, list[0] is still a, b is now pointing to a different object
l.get(0) = b; //error, you can't assign to list.get
I'm in need of help right now. I need to convert 2 dimensional array of numbers to a one dimensional array in Java. Can anyone help me?
Do you mean something like this?
import java.util.*;
public class Test {
public static void main(String[] args) {
String[][] data = new String[][] {
{ "Foo", "Bar" },
{ "A", "B" }
};
String[] flattened = flatten(data);
for (String x : flattened) {
System.out.println(x);
}
}
public static <T> T[] flatten(T[][] source) {
int size = 0;
for (int i=0; i < source.length; i++) {
size += source[i].length;
}
// Use the first subarray to create the new big one
T[] ret = Arrays.copyOf(source[0], size);
int index = source[0].length;
for (int i=1; i < source.length; i++) {
System.arraycopy(source[i], 0, ret, index, source[i].length);
index += source[i].length;
}
return ret;
}
}
If you want it for primitive types, you'll have to write an overload for each primitive type, but you can use new int[size] instead of Arrays.copyOf at that point.
A Java 8 solution could look something like this:
import java.util.stream.Stream;
public class ArrayConverter {
public static String[] flatten(String[][] array) {
// Create a stream of the given array
return Stream.of(array)
// Map each of its elements to a stream (thus creating a
// one-dim-array inside the stream, so to say)
.flatMap(Stream::of)
// Retrieve the stream as array, explicitly calling String to
// keep the type
.toArray(size -> new String[size]);
}
}
I consciously left out generic types in this example since it makes the Array Initialization somewhat confusing. Let me know if you need it tho.
Notably; if you want to use this conversion for Arrays of primitive types you should use the corresponding flat Methods of the Stream Class.
E.g. if you're using int-Arrays use:
Stream.flatMapToInt(...)
to retrieve an IntStream with actual primitive int-Values thus dodging autoboxing into Integer Objects.
JavaDoc of Stream for reference