In order to make a copy of an array that holds a copy of the values instead of by reference I do the following:
int[][][] copy = {{{0}},{{0}},{{0,0}},{{0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0}},{{0}},{{0,0}}};
System.arraycopy(spelerSpel, 0, copy, 0, spelerSpel.length);
then change a value IN THE COPY:
copy[SPELER_NUMMER][0][0] = baanSpelerNummer;
which results at that moment in the ORIGINAL (spelerSpel) array holding the same changed value, e.g.:
{{{4}},{{0}},{{0,0}},{{0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0}},{{0}},{{0,0}}};
as the copy. I also tried .clone(), same result. What am I doing wrong?
By the way I target Android API 8, which doe not support Arrays.copyOf().
System.arraycopy() does not support deep copies, but it does pretty well in terms of performance for simple arrays.
You can use it with some additional loops to create your own multi-dimensional arraycopy3d():
public int[][][] arraycopy3d(int[][][] array) {
int[][][] copy = new int[array.length][][];
for (int i = 0; i < array.length; i++) {
copy[i] = new int[array[i].length][];
for (int j = 0; j < array[i].length; j++) {
copy[i][j] = new int[array[i][j].length];
System.arraycopy(array[i][j], 0, copy[i][j], 0, array[i][j].length);
}
}
return copy;
}
Related
So i am busy with a program where i have this array called names and in the array i have the following data:
public static String [] name = {"Products", "Customers","Calculators","Logout"};
public static String [] subName = {"140 wide Plain", "140 wide Pattern", "280 wide Running width", "280 wide plain drops","280 wide pattern drops"};
now the following code i am trying to implement an if statement:
private List<TitleMenu> getList() {
List<TitleMenu> list = new ArrayList<>();
for (int i = 0; i < names.length; i++) {
List<SubTitle> subTitles = new ArrayList<>();
if (names.equals("Calculators")){
for (int j = 0; j < subNames.length; j++) {
SubTitle subTitle = new SubTitle(subNames[j]);
subTitles.add(subTitle);
}}
TitleMenu model = new TitleMenu(names[i], subTitles, null);
list.add(model);
}
return list;
}
so i only want the second portion of my code to implement if the if statement is correct but i cant seem to get the if statement correct. I am coding in android studio so i dont know how my if statement needs to be
You need to reference the indices of the array. Each index in an array is represented by a number that starts at base zero. This is an important and fundamental concept across many programming languages.
It also looks like your array's name is not names and is name.
So you could check to see if the name is "Calculators" by doing something like this:
if (name[i].equals("Calculators")) { ... }
I have an integer two-dimensional array and I want to store it in SharedPreferences. Could someone show me sample code how to do that? I suppose it is possible if I will convert it to string but it works for one-dimensional integer array. Maybe other way is exist to do that? Thanks.
As you said, converting to string that two-dimensional array will work. However if you face the same problem with more complex objects, you can always convert it to string using the GSON library.
Download: Link
Another post: Link
If you want to pass a two-dimensional array to string, you can iterate through it while storing it to a StringBuffer, as this example:
StringBuffer results = new StringBuffer();
String separator = ","
float[][] values = new float[50][50];
// init values
for (int i = 0; i < values.length; ++i)
{
result.append('[');
for (int j = 0; j < values[i].length; ++j)
if (j > 0)
result.append(values[i][j]);
else
result.append(values[i][j]).append(separator);
result.append(']');
}
result.toString(); //<- Save this in your preference
The situation
I have got different char[] arrays with a different amount of items. To avoid the "OutOfBounds"-Error while processing them I want to standardize them.
For Example:
My Array has following items: "5;9" --> What I want it to look like: "0,0,0,0,5,9" (fill up with "0"s to 6 items)
What I tried:
char[] myarray1 = mystring1.toCharArray();
...
for(int i=0; i<6; i++){
myarray1[i] = 0;
if(i<myarray1.length-1){
myarray1[i] = myarray1[i];
}else{
myarray1[i] = 0;
};
};
My code failed, because it evokes exactly that error...
I hope somebody can help me :)
Thanks!
The reason why your solution doesn't work is that you are using the same array for everything.
After char[] myarray1 = mystring1.toCharArray(); the length of myarray1 is 2, so you cannot simply assign entry 2,3,4 and 5 in your loop. Furthermore if you want the character ´0´ to be in the string, you need to surround your zeros with apostrophes.
You can fix your solution like this:
char[] myNewArray = new char[6];
int myarrayIndex = 0;
for(int i=0; i<6; i++)
{
if(i < (myNewArray.length - myarray1.length)) {
myNewArray[i] = '0';
} else {
myNewArray[i] = myarray1[myarrayIndex++];
};
};
System.out.println(myNewArray); //Will print out: 000059
An easier solution could be this:
myarray1 = String.format("%6s", mystring1).replace(' ', '0').toCharArray();
Firstly trying to fill 0's is not going to fix the error.
Secondly your logic is not right(assuming size as 6), change it to myString.length().
And I don't understand the point of myarray1[i] = myarray1[i];.
Anyways, every array with integer size is initialized by Zero's according to Java specs. On the other hand if you want to fill it with any other value, try Arrays.fill().
I think this function will accomplish what you're trying to do.
private static String formatString(String input)
{
String FORMATTED_STRING = "0,0,0,0,0,0";
int difference = FORMATTED_STRING.length() - input.length();
return FORMATTED_STRING.substring(0, difference) + input;
}
how am I going to move the value of a char array to the same char array? Here's a code:
Assuming ctr_r1=1 ,
for(int ctr_x = (ctr_r1 + 2) ; ctr_x < letters.length - 2 ; ctr_x++)
{
letters[ctr_x] = letters[ctr_x];
}
sb.append(letters);
char[] lettersr1 = sb.toString().toCharArray();
sb1.append(lettersr1);
append the "letters", then convert it to string, then convert it to char array then make it as "lettersr1" value.
what im trying to accomplish is given the word EUCHARIST, i need to take the word HARIST out and place it on another array and call it region 1 (Porter2 stemming algorithm).
The code "ctr_X = (ctr_r1 + 2)" starts with H until T. The problem is I cannot pass the value directly that's why i'm trying to update the existing char array then append it.
I tried doing this:
char[] lettersr1 = null;
for(int ctr_x = (ctr_r1 + 2) ; ctr_x < letters.length - 2 ; ctr_x++)
{
lettersr1[ctr_x] = letters[ctr_x];
}
sb.append(lettersr1);
but my app crashes when i do that. Any help please. Thanks!
I don't understand what you're trying to do, but I can comment on your code:
letters[ctr_x] = letters[ctr_x];
This is a noop: it sets an array element value to the value it already has.
char[] lettersr1 = null;
for(int ctr_x = (ctr_r1 + 2) ; ctr_x < letters.length - 2 ; ctr_x++) {
lettersr1[ctr_x] = letters[ctr_x];
This obviously causes a NullPointerException, since you're trying to access an array which is null. You must initialize the array before being able to modify it:
char[] lettersr1 = new char[someLength];
Additional note: you should choose better names for your variables. The names should tell what the variable represents, and they should respect the Java naming conventions (no underscores in variable names, camelCase). ctr_x, ctr_r1 and lettersr1 don't mean anything.
EDIT:
I'm still not sure what you want to do, and why you don't simply use substring(), but here's how to transform EUCHARIST to HARIST:
char[] eucharist = "EUCHARIST".toCharArray();
char[] harist = new char[6];
System.arraycopy(eucharist, 3, harist, 0, 6);
String haristAsString = new String(harist);
System.out.println(haristAsString);
// or
char[] harist2 = new char[6];
for (int i = 0; i < 6; i++) {
harist2[i] = eucharist[i + 3];
}
String harist2AsString = new String(harist2);
System.out.println(harist2AsString);
// or
String harist3AsString = "EUCHARIST".substring(3);
char[] harist3 = harist3AsString.toCharArray();
System.out.println(harist3AsString);
May be so:
String str = "EUCHARIST";
str = str.substring(3);
and after toCharArray() or smth another
I'm tring to do a simple matrix initialization in android and I get Error: java.lang.ArrayIndexOutOfBoundsException. I'm trying this:
Integer id = Integer.valueOf(idcateg);
System.out.println("Id-ul e"+id);
vot = new Integer[sirid.length][id];
for (int i = 0; i < sirid.length; i++) {
vot[i][id] = 0;
}
where id is a value between 1 and 5,sirid.length is a number that reflects number of images from different categorys. For example,I want for category 1 to have something like this :
vot[0][1]=0;
vot[1][1]=0;
vot[2][1]=0;
...etc
Where is my mistache?
try this
Integer id = Integer.valueOf(idcateg);
System.out.println("Id-ul e"+id);
vot = new Integer[sirid.length][id];
for (int i = 0; i < sirid.length; i++) {
vot[i][id-1] = 0;
}
array index start from 0
you set the size of the vot array to sirid.lengh by id but the array start index value from 0 to (size)(size value not included) see the for loop
I think because you initialize the array on id.
After that you call to vot[i][id] which then will alway be 1 too high.
f.e.
if you create new int[3][3]
you can only call positions 0,1 and 2
good luck
That is because id has been considered as Length of the String and you r trying to access the same element... but the last element is vat[i][id-1] but u r trying to get vat[i][id]
So better use this..
for (int i = 0; i < sirid.length; i++) {
for(int j = 0; j<id ; j++){
vot[i][j] = 0;
}
}