Does anyone know why this doesn't compile?
public class ArrayCompare{
public static void main (String []args){
String words= "Hello this is a test";
String delimiter=" ";
String [] blocker=words.split(delimiter);
String [][] grid= new String [100][100];
grid[0]="Hello";
if (grid[0].equals(blocker[0])){
System.out.print("Done!");
}
}
}
I would like to perform this function of comparison using a 2 dimensional array. I am a newbie! Please help if you can. Thanks in advance!
Try this:
grid[0][0]="Hello";
grid is a two-dimensional array. For the same reason, you need to do this:
if (grid[0][0].equals(blocker[0]))
grid[0] is a String[] type, not a String. So your code should be like this
grid[0] = new String[100];
grid[0][0] = "Hello";
if (grid[0][0].equals(bloker[0])) {
//your logic...
}
String [][] grid= new String [100][100];
grid[0]="Hello";
There's your problem. You are trying to assign a string to a string array. Think of a 2d array as an array of arrays.
You probably want something like
grid[0][0]="Hello!";
grid is 2d array. you can't do like d[0] = "Hello". So, if you want to assign value at 0 position
d[0][0] = "Hello";
if (grid[0][0].equals(blocker[0])){
System.out.print("Done!");
}
It won't compile because grid[0] is not String type.
It is String[] (Array) type. Variable grid[0] is actually pointing to a String[100] array.
You are attempting to assign a string "Hello" to an array by
grid[0]="Hello"; statement.
If you want to assign a string to a location in grid, you must provide two index(s) - following is legal:
grid[0][0]="Hello";
May I suggest using eclipse or BlueJ to edit your Java code? so that such basic errors are shown on real-time and explained well?
First thing you can't assign value to the element of the multi dimensional array using single index
grid[0]="Hello";
you need to specify both the indices like grid[0][0] = "Hello"
this will set the 0th element of 0th row to Hello
Likewise while compairing
if (grid[0].equals(blocker[0])){
System.out.print("Done!");
you have to pass the same indices here (You cannot compare a String to a Array Object)
if (grid[0][0].equals(blocker[0])){
System.out.print("Done!");
Related
I want to add a String to a specific location in an ArrayList that looks like this:
ArrayList <String[][]> arrayList3D = new ArrayList<>(Arrays.asList(arrayString3D));
I tried this out:
arrayList3D.get(0).get(1).add("new Word");
but it didn't work...
Man, first you should create an array and later the another. try this.
ArrayList<ArrayList<String>> arrayList3D = new ArrayList<ArrayList<String>>();
Later, you should create the another.
arrayList3D.add(0, new ArrayList<String>());
but you show that you want to do this.
arrayList3D.get(0).get(1).add("new Word");
The problem here is that does it exist a value in that position. It does, it works, but, it doesn't.. you should write this.
ArrayList3D.get(0).add(1, "value to input");
You're close but not quite correct.
The process goes as follows:
arrayList3D.get(0) regardless of the index provided ( 0 or greater) will return a 2D array i.e String[][].
so in order to access a particular position of the 2D array, you'll need to use 2 pairs of square brackets one indicating the row and another indicating the column.
i.e
arrayList3D.get(0)[1][0] = "new Word";
Arrays in Java don't provide get methods. An equivalent is given by bracket notation. You set the element at index i like:
array[i] = value;
Your ArrayList contains elements of type String[][] which are arrays that contain other arrays that hold String elements.
So a correct access would look like:
arrayList3D.get(0)[1][i] = "new Word";
Where i is the position you want to add the element in the last array.
Maybe this view helps more:
arrayList3D // ArrayList<String[][]>
.get(0) // String[][]
[1] // String[]
[i] // String
= "new Word";
If you want to have get methods and be able to dynamically add elements, you would need something like ArrayList<List<List<String>>> instead since arrays are of fixed size.
You could do it by manually converting your String[][][] to List<List<List<String>>>, for example by using regular loops:
List<List<List<String>>> arrayList3D = new ArrayList<>();
// Traverse all 2-dim elements
for (String[][] dim2Arr : arrayString3D) {
List<List<String>> arrayList2D = new ArrayList<>();
// Traverse all 1-dim elements
for (String[] dim1Arr : dim2Arr) {
List<String> arrayList1D = Arrays.asList(dim1Arr);
// Add 1-dim to 2-dim
arrayList2D.add(arrayList1D);
}
// Add 2-dim to 3-dim
arrayList3D.add(arrayList2D);
}
having a parse issue here on bottom line can someone please help!
FileIO io = new FileIO();
String[] original = io.load("C:\\sharePrice.txt");
int numcols=original[0].split("\t").length;
double[]sharePriceArray = new double[numcols];
for(int i=0;i<numcols;i++)
{ //load in the data
sharePriceArray[i] = Double.parseDouble(original[i].split("\t"));
}
AFAIK, .split splits a string into a string array. Your code is trying to assign a double to a string array, so that's the reason of your parse issue.
To fix, I would reccomend splitting the string into an array using .split(), and then traversing that array to assign to the double.
I suppose this should be like original[i].split("\t")[0] or original[i].split("\t")[1] in this case you are taking first or second value and then do parsing.
I need to check if a specific variable is in a very large array of Strings (1000+), however doing it with just a for loop and comparing each time would be slow. Is there an alternate method to the way I am using below?
String[] easyWords= new String[]{"integer","project","octopus"}; //+1000s more words
String easyrnd = (easywords[new Random().nextInt(easywords.length)]);
String letterguess = consolereader.nextLine();
for(int i=0;i<easyWords.length;i++){
if(letterguess==easywords[i]){
// do something
}
You can use easyrnd.contains(letterguess) to check if letterguess exists in easyrnd.
So I initialized an array lets say
`string example = new string [5];
When I called the split method on a line of
string x = "abc, def, g";
example = x.split(",");
example[0] = abc
example[1] = def
example[2] = g
I can no longer access example[3] and example[4] as I am getting null pointer
shouldn't these still be accessible with values of null?
Even though you created a array with some initial length for example
String [] sample = new String[5];
after assigning new Array to the variable It will create a new array with new array size. for example.
String s = "Hi how are you";
sample = s.split(" ");
so you can not access old array elements.
It doesnt preserve the values. "example" is assigned with a completely new and different array.
If you want to preserve the number of elements previously present in the array, you can do something like:
int num = example.length;
example = x.split(",");
example = Arrays.copyOf(example, num);
// initialize the new array elements here.
Of course doing so is not very efficent and should be avoided. I suggest you take a look at array lists instead.
Your initial example array (of length 5 with null values) gets overwritten by the new array returned by the call to the split() method, in this case an array of length 3. The initial array referenced by the example reference is not accessible anymore, it will be garbage collected.
If you want to keep both arrays you can assign the result of the split to another variable
String[] example2 = x.split(",");
i have a strings array.
i want to check the length of a string contained in X cell in that array,
in order to create another array with the length of that string.
i've tried using:
public static String[] originalToPrint = new String[10];
then getting strings from the user that will enter the strings array,
then:
int[] temp = new int[ originalToPrint[y].length() ];
but i get a NullPointerException
do you have any ideas?
thanks
The code looks fine, it seems you didn't write any string to the position originalToPrint[y], so you get a nullpointer when you accesss it and call .length() on null. You should post the method where you fill the array originalToPrint and where you get your index y from.