Problema con arreglo de strings

Edy
28 de Agosto del 2004
Hola!
A ver si alguien me puede ayudar.
Tengo un arreglo de string (String[][]) que se lo asigno a otro arreglo de string
String[][]a=new String[2][2];
String[][]b=a;
si modifico b, se refleja en a y quiero que a no se modifique.
¿Me ayudan?
Gracias
Edy

Rigel
28 de Agosto del 2004
Hola:

Bueno según lo que te entendí es que no quieres que se modifique el arreglo String [][]a, es decir que sea como una constante,,, si es así entonces a la variable a agregale final, es decir: final String[][]a = new......
con esto no se verá afectado el String a por ninguna otra variable.. Espero que halla entendido tu duda.

SALUDOS

Edy
28 de Agosto del 2004
Tambien intenté con esto,
public static void main(String[] args)
{
final String[][] sView = new String[2][2];
sView[0][0]="12"; sView[0][1]="3"; sView[1][0]="43";
sView[1][1]="62";
if(sView.length>0){
int y = sView.length;
int z = sView[0].length;
String[][] tskList =sView;
for (int m = 0; m < y; m++) {
for (int n = 0; n < z; n++) {
System.out.println("tskList[" + m + "][" + n + "]=" + tskList[m][n]);
}
}
tskList[1][0]="aa";
tskList[1][1]="ss";
for (int mm = 0; mm < y; mm++) {
for (int nn = 0; nn < z; nn++) {
System.out.println("sView[" + mm + "][" + nn + "]=" + sView[mm][nn]);
System.out.println("tskList[" + mm + "][" + nn + "]=" + tskList[mm][nn]);
}
}
}
}

Esto imprime:
tskList[0][0]=12
tskList[0][1]=3
tskList[1][0]=43
tskList[1][1]=62

sView[0][0]=12
tskList[0][0]=12
sView[0][1]=3
tskList[0][1]=3
sView[1][0]=aa
tskList[1][0]=aa
sView[1][1]=ss
tskList[1][1]=ss

¿Que hago?
Gracias

Maggy
28 de Agosto del 2004
hola creo que esto soluciona tu problema, ya lo probe y si funciona.

El problema que tienes es por que cuando igualamos una variable a otra en java, lo que hace es asignarle el mismo lugar de memoria a la variable nueva, por lo tanto al modificar una modificamos tambien la otra.

Estas son las modificaciones que le hice a tu codigo y tambien te muestro la salida que me da.


public class ejemplo_web
{

public static void main(String[] args)
{
final String[][] sView = new String[2][2];

sView[0][0]="12"; sView[0][1]="3";
sView[1][0]="43"; sView[1][1]="62";

if(sView.length>0){
int y = sView.length;
int z = sView[0].length;
String[][] tskList =new String[2][2];

//no hago esto ya que les estaria asignando la misma
//localidad de memoria a los dos
// y al modificar uno estoy modificando el otro
//tskList = sView;

for (int m = 0; m < y; m++) {
for (int n = 0; n < z; n++)
{
//System.out.println("tskList[" + m + "][" + n + "]=" + tskList[m][n]);
//aqui inicializo uno a uno los valores del arreglo
tskList[m][n]= sView[m][n];
}

tskList[1][0]="aa";
tskList[1][1]="ss";
for (int mm = 0; mm < y; mm++) {
for (int nn = 0; nn < z; nn++) {
System.out.print(" sView[" + mm + "][" + nn + "]=" + sView[mm][nn]);
System.out.println(" tskList[" + mm + "][" + nn + "]=" + tskList[mm][nn]);
}
}

}

}
}
}


Y la salida que me da es la siguiente:

sView[0][0]=12 tskList[0][0]=12
sView[0][1]=3 tskList[0][1]=3
sView[1][0]=43 tskList[1][0]=aa
sView[1][1]=62 tskList[1][1]=ss
sView[0][0]=12 tskList[0][0]=12
sView[0][1]=3 tskList[0][1]=3
sView[1][0]=43 tskList[1][0]=aa
sView[1][1]=62 tskList[1][1]=ss

Espero que te sirva...
Bye ;)

Maggy..ta


Kurz
28 de Agosto del 2004
lo que tienes que haces si quieres que los cambios que hagas en uno no se reflejen tb en el otro, es usar el metodo clone().

Edy
28 de Agosto del 2004
Ya intenté, pero no funcionó. Hice esto:

public static void main(String[] args)
{
String[][] sView = new String[2][2];
sView[0][0]="12"; sView[0][1]="3"; sView[1][0]="43";
sView[1][1]="62";
if(sView.length>0){
int y = sView.length;
int z = sView[0].length;
String[][] tskList = (String[][])(sView.clone());
for (int m = 0; m < y; m++) {
for (int n = 0; n < z; n++) {
System.out.println("tskList[" + m + "][" + n + "]=" + tskList[m][n]);
}
}
tskList[1][0]="aa";
tskList[1][1]="ss";
for (int mm = 0; mm < y; mm++) {
for (int nn = 0; nn < z; nn++) {
System.out.println("sView[" + mm + "][" + nn + "]=" + sView[mm][nn]);
System.out.println("tskList[" + mm + "][" + nn + "]=" + tskList[mm][nn]);
}
}
}
}

Y me imprime :
tskList[0][0]=12

tskList[0][1]=3

tskList[1][0]=43

tskList[1][1]=62

sView[0][0]=12

tskList[0][0]=12

sView[0][1]=3

tskList[0][1]=3

sView[1][0]=aa

tskList[1][0]=aa

sView[1][1]=ss

tskList[1][1]=ss

Kurz
28 de Agosto del 2004
He estado mirando tu problema, ya siento haberte dado una solucion sin haber comprobado antes su validez, y lo unico que he encontrado x ahora es lo siguiente:
Haz un bucle y y haz una asignacion de todos los campos del String[][], en tu caso:
stkList[i][j] = sView[i][j]