Put String[] in a String[][]? - java

Ok, soo what I'm trying to do, I have an oject with a attribut which looks like String[][], and I want to fill this one by calling a function and fill this String[][], one by one.
So here is what I tried but I get an error telling me :
"The type of the expression must be an array type but it resolved to String"
When I try to do
Produit[NbrProduit][0] = Produit[0];
My code :
public String[][] Produit = new String[MAX_Produit][2];
public void GetInfo1(String Client, String[] Produit,int NbrProduit){
Produit[NbrProduit][0] = Produit[0];
Produit[NbrProduit][1] = Produit[1];
Produit[NbrProduit][2] = Produit[2];
I don't understand why I get this because I'm filling a String field with an another String, right ? no ?
Sorry for my english.

Your parameter name (a 1D array) is the same as your instance variable name (a 2D array), so you're actually referring to your parameter only. Either use a different name (recommended), or use this.Produit when referring to your 2D array.
i.e.
public void GetInfo1(String Client, String[] produitParam,int NbrProduit){
Produit[NbrProduit][0] = produitParam[0];
Produit[NbrProduit][1] = produitParam[1];
Produit[NbrProduit][2] = produitParam[2];

Produit is a two dimensional String array, which means each of its element will be a string array. So you need to assign the element with the array of String and not just one String. Try this if it makes sense for your logic:
this.Produit[NbrProduit][0] = Produi;

Related

How to make a reference to the Variable in java and not to the Type of it

I have a specific question, so I start with an example:
I have some values of type String saved in variables as follows:
String one = "someValue";
String two = "anotherValue";
String three = "the last one";
and so on. This is of course a simple example, in the fact it will be methods that returns String Type and I save it in Variables to use further in code;
I also have a List of Strings that contains some namespaces, that should be a name of xml-elements:
List<String> namespaces = new ArrayList<Sting>(Arrays.asList("example:org:one", "example:org:two", "example:org:three"); //and so on
It will be seems as
<example:org:one></example:org:one><example:org:two></example:org:two><example:org:three></example:org:three>
in xml - document. But of course they also have some values, which are saved in String Variables ('one', 'two', 'three' respect.) and I would like to make a reference on it in Java code, but explicitly, not implicitly. Say, use as value 'one' -> but not a String 'one', but a Variable, which name is ONE. GOT THE IDEA?:
for(String s : namespaces) {
String[] arrayOfNamespaces = s.split(":");
int lengthOfArrayOfNamespaces = arrayOfNamespaces.length;
xmlBuilder.with(s, arrayOfNamespaces[lengthOfArrayOfNamespaces - 1]);
}
Is it possible? Is there something in java, that make a reference to a variable with the particular name in code: I believe it would be as something like Casting to VAR or as Annotation (maybe somebody had made something like this) :
...
xmlBuilder.with(s, #Var arrayOfNamespaces[lengthOfArrayOfNamespaces - 1]);
Or Mapping is the only way to make it works?

Using static array

I am novice programming. I am using java. I have declared an array like this:
static String horario[];
later, in one method I want to use this array like this:
if(datos.get(z+3).contains("CET")) {
horario[]= (datos.get(z+3).split("CET"));
mipartido.setHorario(horario[0]);
}
but it says that horario cannot be resolved to a type.
How can I use this variable?
You shouldn't use [] unless you're assigning an element to a specific index of the array, for example: horario[0] = "abc";
So, since you're assigning the array, you should change:
horario[]= (datos.get(z+3).split("CET"));
to:
horario = (datos.get(z+3).split("CET"));

using split on Array Object in Java

Why do I get mssg with "cannot find symbol", when I use .split on the String Object?
public static void main(String[] args) {
String[] inputFile = StdIn.readAllLines().split("\\n");
//create Congress caracteristics
int states = Integer.parseInt(inputFile[0]); // read states value and save in the first
//position in the array
int totalSeats = Integer.parseInt(inputFile[1]); //read number of seats and save in the second
//position in the array
This is because you are trying to call the split method on an array. There is no such method on an array. readAllLines() already returns an array of the strings. Just remove the .split() call.
From the JavaDoc:
static String[] readAllLines()
Reads all remaining lines from standard input and returns them as an array of strings.
StdIn.readAllLines() does not return a String but a String[] (array of strings). You can't use split() on an array of strings.
Actually, you don't need to do that, because calling readAllLines() already gives you the array you are looking for.
You are most likely calling #split() in an Array/Collection of Strings, not a String. You should iterate though them and split one after the other.
We have fileName Object we itrate it a split it simple in this program i split my object with (.) so thats why i used this =split("\\.");
String[] fileName="we have a object here to we want to splite it ";
for(String obj:FileName)
{
String[] part1=obj.split("\\.");
}
System.out.println(part1);
check it its working fine

Is there a way to initialize a list of variables using an array in Java?

Is there a syntax in Java to initialize a list of variables to corresponding objects in an array?
String hello, world;
String[] array = {"hello", "world"};
//I want:
{hello, world} = array;
//instead of:
hello = array[0];
world = array[1];
I think I recall this type of convenient syntax from Matlab, but I haven't noticed a way to achieve this in Java.. This kind of syntax would help me organize my code. Specifically I would like to feed into a function an array of objects in a single argument instead of each of the array's members in multiple arguments, and then begin the code for the method by declaring variables in the method scope for named access to the array members. E.g.:
String[] array = {"hello", "world"};
method(array);
void method(array){
String {hello, world} = array;
//do stuff on variables hello, world
}
Thanks for the advice. -Daniel
Nope, there is no way to do that in Java, other than the answer you already gave, which is to initialize each variable separately.
However, you could also do something like:
String[] array = { "hello", "world" };
final int HELLO = 0, WORLD = 1;
and then use array[HELLO] or array[WORLD] wherever you would have used the variables. It's not a great solution, but, then again, Java usually is verbose.
Specifically I would like to feed into a function an array of objects
in a single argument instead of each of the array's members in
multiple arguments
This seems like a case where you should be using an object instead of an array. Specifically because in your example it seems like you're using an array to represent an object with two fields, hello and world:
class Parameters {
String hello;
String world;
public Parameters(String hello, String world) {
this.hello = hello;
this.world = world;
}
}
//...
Parameters params = new Parameters("hello", "world");
method(params);
//...
void method(Parameters params) {
// do stuff with params.hello and params.world
}

How can I put a Java array inside itself?

I'm attempting to create a Java object array and place the array inside itself at its second index (in order to represent a self-similar fractal with the array), but when I try to access theArray[1][1][0], I get this error:
Main.java:11: error: array required, but Object found.
This is what I've tried so far, and I'm not sure why it's not working:
import java.util.*;
import java.lang.*;
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
Object[] theArray = new Object[2];
theArray[0] = "This array should contain itself at its second index.";
theArray[1] = theArray; //Now I'm attempting to put the array into itself.
System.out.println(theArray[1][1][0]) //Main.java:11: error: array required, but Object found
}
}
Is it actually possible to put a Java array inside itself, as I'm attempting to do here?
theArray[1] is of compile-time type Object (since it comes from an array of Objects).
You need to cast it to Object[] to use it as an array.
The fundamental problem you're encountering is that although an array that contains itself is a perfectly valid object, it isn't a valid type.
You can nest array types arbitrarily deeply – Object[][][][][][][][][][][][][] is a valid type.
However, the "bottom level" of the type can't be an array.
You're trying to create a type which is an array of itself.
Using generics, that would be possible:
class Evil extends ArrayList<Evil> { }
You're running into a casting error since you've declared theArray to be an Array of Objects. As a result, you can't promise Java that theArray[1] is an Array--it could be any kind of Object. You'll need to break up your access to do what you want:
Object[] innerArray = (Object[]) theArray[1];
System.out.println(innerArray[0] == theArray[0]); // Always true since innerArray IS theArray
while (true) {
// Careful! This loops forever!
// set innerArray = innerArray[1] = theArray = theArray[1] = innerArray...
// all of these are the exact same object (but you have to tell Java their type every time)
innerArray = (Object[]) innerArray[1];
System.out.println(innerArray[0]);
}
Your code is equivalent to
Object arr = theArray[1]; // arr is an Object here, not an array
But you could do
Object[] arr = (Object[] ) theArray[1]; // Now it is an array
This can be done quite easily with ArrayLists:
ArrayList list = new ArrayList();
list.add(list);
So now
System.out.println(list.get(0));
and
System.out.println(((ArrayList)list.get(0)).get(0)); //Casting because .get() returns an Object
Both will output the same thing.
You can take this to an arbitrarily large number of levels, if you want to:
System.out.println(((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)((ArrayList)list.get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0)).get(0));

Categories