I'm looking for a quick solution about an issue in my following code. It is working but not how I wanted.
So,
the idea is that I'm trying to pass an Character class array and convert it into ArrayList. Since I'm using generics, I specified:
public void fromArrayToCollection(T[] a,Collection e)
and I guess this means I can pass what kind of arrays I want, such as Int array, char array,String array,double and so on
But it won't let me pass an character array.
If I am passing a String array is it actually working
Why?
Thank you.
public class FuDaBi {
static class StringToArray{
public <T> void fromArrayToCollection(T[] a,Collection<T> e)
{
for(T x: a)
e.add(x);
}
public <T> void fromStringToArray(T inputString, T[] array )
{
}
}
public static void main(String[] args)
{
Character[] my_array = {'S','A','L','U','T'};
ArrayList<String> result = new ArrayList<String>();
String daniel[] = {"WV","Mercedes"};
StringToArray myimp = new StringToArray();
myimp.fromArrayToCollection(daniel,result);
for(int i=0;i<result.size();i++)
{
System.out.println(result.get(i));
}
}
}
The problem is that I made the ArrayList a String type instead of an Character.
ArrayList result = new ArrayList();
Related
I know how to create a method,
which takes an array
public void x(int [] arr){}
but I do not know how to set a given length to that array.
If you are trying to make a method that accepts an array, you can structure it like this:
public void x (int[] arr) {
// Code
}
If you want to restrict the length of array the method can accept, you could do something similar to this:
public void x (int[] arr) {
//Check to see if arr is a satisfactory length
if (arr.length > 10) {
//Throw an exception, break, print to the console etc...
}
}
There are some areas where I can't make this explanation as detailed because I don't know what language you're programming in.
first I use annotation to receive some params, String and String[], e.g.
#Retention(RetentionPolicy.RUNTIME)
#Target(ElementType.METHOD)
#Documented
public #interface RedisCacheAble {
String value() default "";
String[] names() default {};
}
#RedisCacheAble(value="XXOO",names = {"a","b"} )
public OrderDetailPO orderTestAble(String op) {}
then my manager said that the value and names must use const because other place may use this values, so I change my code like that:
public static final String XXOO = "xxoo";
public static final String XOARR = {"orderCode","accountId"};
#RedisCacheAble(value=XXOO, names = XOARR )
public OrderDetailPO orderTestAble(String op) { //
}
unfortunately the eclipse throw a error: XOARR must initial as an Array, so it's seems annotation can not recognized a const array, any one know why?
Because annotation attribute value must be const. But even your array is final, its conent is also changeable. So it's not annotation can't recognize const array, there is no const array.
There are methods like writeDoubleArray and so on in ObjectDataOutput interface. Of course arrays can be of any dimension and in my case there are some 2D double arrays. How to serialize and then deserialize those? Or maybe because of the way Java handles 2D arrays (not really contiguous blocks in memory) I need to represent a 2D array as 1D one? I mean because of the performance I shouldn't really serialize 2D arrays. Just wrap it into utility object and store like this...?
You have to put the other dimensions yourself first, like the following snippet:
public static class Foo implements DataSerializable {
private double[][] multiDimArray;
#Override
public void writeData(ObjectDataOutput out)
throws IOException {
int firstDim = multiDimArray.length;
out.writeInt(firstDim);
for (int i = 0; i < firstDim; i++) {
out.writeDoubleArray(multiDimArray[i]);
}
}
#Override
public void readData(ObjectDataInput in)
throws IOException {
int firstDim = in.readInt();
multiDimArray = new double[firstDim][];
for (int i = 0; i < firstDim; i++) {
multiDimArray[i] = in.readDoubleArray();
}
}
}
That way it is possible two write any depth of array into a stream. You might be able to abstract that a bit away into two methods (read/write) for easier / more convenient use.
That's my question.
Thank you!
Encapsulation is good because you can protect the values of variables and what variables can be seen/edited.
Here's an example in Java:
public class Foo {
private int size;
private String name;
public int getSize() {
return size;
}
public void setSize(int size) {
if (size > 0)
this.size = size;
}
public String getName() {
return name;
}
public void setName(String name) {
if (name == null || name.isEmpty())
return;
this.name = name;
}
}
This makes sure that the variable size is greater than 0 and the value of name isn't empty or null. You can also put checks in the constructor.
One more thing. If you had made the variable size public, and everyone used fooObject.size=232, and then suddenly you wanted to make restrictions, it would break everybody's code. It's better just to start by using encapsulation.
Hope this helps :)
I want to have an array which randomly gives me one of the given answers. But Java says "array[hallo] not possible; you use String but int is expected"
Is there a possibility to solve this problem or am I all wrong?
import java.util.Random;
public class ZufallsAntworten
{
private Random ran;
public ZufallsAntworten()
{
ran = new Random();
}
public int richtigeAntwort()
{
String[]array = new String[100];
array[0]="a";
array[1]="b";
array[2]="c";
array[3]="d";
int hallo = ran.nextInt(array.length+1);
return array[hallo];
}
}
Your method richtigeAntwort() returns an int, but you are returning a String, since your array is an array of Strings. This line:
return array[hallo];
will fetches one of the Strings from your array at that index and return it.
I cannot read the name of your method so I cannot advice you much beyond simply changing the return type of the richtigeAntwort() method to String.