Is ["abcd"] and [["abcd"]] are same? [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have used java ArrayList when I inserted one element in the list and i have converted the validatable response to array list when I used assert equals it is showing that both are different
like one is [abcd] other is [[abcd]]
Validatable response = given().spec(request).filter(new ErrorLoggingFilter(errorPrintStream)).pathParams("","").when.post(endpoint).then()
the response is of the form ArrayList when I printed that It came of the form [[abcd]]

To my knowledge, these two are different things
["abcd"] this means an array has one string "abcd" element.
[["abcd"]] this means an array has one array ["abcd"] element.

Yes, ["abcd"] and [["abcd"]] are completely different. Let us understand why.
Let us consider an array ["abcd"]. As you can see, it contains only one element i.e. "abcd". So this is an array that contains a single string value. Now for [["abcd"]], the outer array contains another array inside of it and the inner array contains "abcd". Though their ultimate content seem to be same, they are absolutely different. One is a string array (an array that contains a string value) and the other is an array of string arrays.

Absolutely different, a one-dimensional array, a two-dimensional array,in many languages,reference form it, [0] and [0][0]

Related

How to add different values to an arraylist? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have a method that randomly selects objects from an arraylist. Those objects are being added to another arraylist I created. How can I make it so that the same object is not added twice?
Let's say I have an arraylist:
["Chicken", "Dinner", "Noodles"]
I have another arraylist that I want to add values randomly from the first arraylist:
[]
So I use:
Math.random to get values from 0-2 and add them to the new arraylist.
Let's name the empty one list2 and the top one firstlist
So:
Assuming integer i is a random number 0-2.
list2.add(firstlist.get(i));
Yet this has a chance to add the same value, how can I check through list2, to make sure it hasn't been added yet, so I can not add it and pick another value?
Replace the other list with a java.util.Set. If your objects have correct hashCode() and equals() then they won't be added twice. In other words, use the right collection type for the job.

I need 2 denominational array sorted [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am working on a project that has a list of student name and numbers, for example
James Bloggs,1
Paul Jonson,43
Andt Peters,23
Once I have them in an array I then need them sorted.
What is the best way of going about this. Its not the sort Im stuck on its the referencing the names to the numbers. I would have thought if I do a 2 denominational array only one would be sorted.
Any help would be great,
EDIT: I just realized this question was asking about a 2-dimensional array and my answer doesn't directly deal with that. I am skeptical that arrays should be involved at all. Arrays are usually for dealing with primitive data, and maybe if you are coming from a C background you'd think they'd be the natural thing to use. If you really honestly have to use arrays then this probably isn't the way to go.
https://docs.oracle.com/javase/7/docs/api/java/util/TreeMap.html
public void foo(){
// Use a TreeMap. It will sort keys on insertion.
Map<Integer,String> nameByNumber = new TreeMap<>();
nameByNumber.put(1, "James Boggs");
// etc. put all the entries in however you need to
List<Integer> sortedNumbers = personByNumber.getKeys();
List<String> namesSortedByNumber = personByNumber.getNames();
}
If you need it to be more organized and complex, you can encapsulate the name and number into a Class with a name and number property. Then you'd still use the number as the key, but you'd have the full class as the value. Do this if you need to have more than just a name, like last name, first name, address, etc.

two arrays are always equal [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have this code
for (int i=0; i<tini.length; i++){
tini[i].tempLabel.setText("Temp: "+ Float.toString(tempArray[i]) +"°" );
out_status[i] = tini[i].alarm;
frame.statusLabel.setText("Connetction: OK, String: OK");
}
System.out.println("old: " + Arrays.toString(out_status_old));
System.out.println("new: " + Arrays.toString(out_status));
if (Arrays.equals(out_status, out_status_old) ){
System.out.println("UGUALI");
}
out_status_old = out_status;
the resulting arrays are always equal. I cannot understand the reason.
Using a Button in JFrame, in a GUI interface i can modify the value of alarm, but both the old value and the actual one change at the same time!
When you access and then update the elements of one array, you're also updating the elements of the other array because they are referencing the same objects. You need to create separate items within each array while populating the arrays.
You left out an important part of your program which is where you're actually populating these arrays. Odds are, you are not doing a deep copy.
Deep copy of an object array
The line out_status_old = out_status; does not create a copy of the array. You have just two variables, out_status and out_status_old, pointing to the same array.
If you want to create a proper copy of the array, you can e.g. use Arrays.copyOf (or one of its variants).

Why do we need two dimensional array? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
Why do we need two-dimensional or multi-dimensional array?
If we want a series of continuous elements of the same type, it can be achieved with simple 1-D array. Like int[] a = new int[15625] will allocate space 15625 elements, and
int [][][] b = new int[25][25][25] will also allocate space 15625 elements
Why do we need them if things can be achieved with 1-d?
You don't need them, but doing grid[x][y] is nicer than grid[x + y*width]
Because sometimes data isn't one dimensional.
For example, the classic game breakout. You could organize your bricks into a one dimensional array and each turn process which row they are in, however it makes more sense to make it a two-dimensional array where the first array is the rows, and the second array is the bricks.
i.e:
bricks[0][3] is the fourth brick of the rirst row
While this could still be done with a one dimensional array, there are math applications where multi-dimensional arrays are necessary.
In addition to this it allows you to have arrays of an infinite1. size, whereas arrays are limited to 230
1. mileage may vary
As mentioned in the comments, multi-dimensional arrays in Java are actually nested arrays. Your nested arrays don't all need to be the same size which is one reason you would use them instead of a single array as you have proposed.
The only other way to declare such an array (off the top of my head) is to declare an array of type Object and then put arrays in it but you lose the basic array type safety. You can access the elements in nested arrays without the [][]... syntax, however.

get a certain part of a hashmap [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm converting a hashmap into 2 arrays, an integer array, and a String array. I'm trying to do this to make it usable, my HashMap looks like this:
HashMap<String, String> I'm converting the second string into an integer, and the hashmap is created from what the user puts into the configuration file.
I want to move the first string into an array, and then the second string into another array, how can do that?
in otherwords, when i do hashmap.values() is there a way to get one section, like the first and the the second one later? What's the best way around this?
I thing you basically want to create two arrays one for keys and one for values. You can use map.keySet() to get keys and map.values() to get values. Now you can convert this two into a List or Array.
I want to move the first string into an array, and then the second
string into another array, how can do that?
List<String> keyList= new ArrayList<String>( map.keySet());
List<String> valueList= new ArrayList<String>( map.values());
If you want to convert List as array than,
String[] keyArray=keyList.toArray(new String[keyList.size()]);
String[] valueArray=valueList.toArray(new String[valueList.size()]);

Categories

Resources