I have two arrays "tags" and "selected" the tags array is static and depending on what the user selects the "select" array values will be set to true or false.
I want to compare the tags array and the select array and the "select" array positions that have true I want to pull out the correspoding position of the "tags" array and bulid a new array.
private String[] tags = new String[] { "Bob", "Tom", "Mike", "Smith" };
private boolean[] selected = new boolean[tags.length];
public String[] selected_tags;
for (int i = 0; i < tags.length; i++) {
if (selected[i] == true){
selected_tags[i] = tags[i];
}
I am not sure if I am doing this correctly because I feel like I would have empty spots in my selected_tag array. If there is a better way of doing this I am open to suggestions.
thanks!
public String[] selected_tags = new String[tags.length]
Everything you say and you did is fine and reasonable. Just make sure all of them are of the same size. (see above)
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);
}
I have to put in a string array some values resulting from several parsed html pages. So the first value it's a name and all the others are numbers. After I must return the array to main to print. Obviously I make something wrong .
this is part of my newbie code...
String[] ret = null;
int y = 0;
for (Element h1 : h1s) {
// Using Jsoup to scrape the html file and find H1 text
h1_id = h1.className();
// I put here the text of H1
h1_text = h1.text();
if (h1_id.equals("ezomat-logo-text ezCSS")) {
// jump to the next h1
} else {
// I want to put the txt as the first array place
ret[y] = "'" + h1_text + "'";
}
i = 0;
// found the number values single integers with comma
for (Element image : images) {
Imm[i] = "," + imageName;
i++;
}
i = 0;
y = 1;
// y = 1 because I want to start from the second position.
for (Element image : images) {
ret[y] = Imm[i];
i++;
y++;
}
}
return ret;
You can't dynamicly resize an array, you have to initialize it with a fixed size.
So, you have to initialize it with
String[] ret = new String[size];
where size have to be the number of elements you are going to put into your array.
Or the better approach: Use ArrayList<String>instead. Initialize it with
ArrayList<String> ret = new ArrayList<String>();
and add your Items with ret.add("whatever");.
On the first line of your code you attempt to define an array without a size, but you don't actually define it, you just assign null.
Also, it's impossible to dynamically add elements to such array.
For these scenarios we have List.
To define a List that stores Strings use the following code:
List<String> ret = new ArrayList<String> ();
And then proceed to add elements to this array like so:
ret.add ("," + imageName);
To retrieve a value from an index in the list do the following:
ret.get(index);
Java does not allow arrays with variable length. I think that this is your main problem.
There are two choiches:
Obtain the array length first and instantiate the array accordingly
String[] ret = new String[100];
Use an ArrayList
ArrayList<String> ret = new ArrayList<String>();
You can add elements to the ArrayList like this: ret.add(value);
The Java Tutorial: Arrays
java.util.ArrayList reference
I have a list of countries where the product is available. I am looking for the code to check whether any duplicate value exists in the list of countries.
They are giving like this: AZ,CA,GB, AU,AR,AT,MX,NL,NZ.
How do I check in Selenium if a duplicate value exists with a loop?
As others have pointed out, this has very little to do with Selenium, other than where your values come from.
I will assume that your list of countries comes from a pulldown menu. If not you will need to adjust the code below to match.
import java.util.*;
...
Select slctCountry = new Select(driver.findElement(By.id("select_id")));
// create an empty List
List<String> optionsList = new ArrayList<String>();
// a Set naturally removes duplicates!
Set<String> optionsSet = Collections.emptySet();
for (WebElement option : slctCountry.getOptions()) {
// fill both from the same source
optionsList.add(option.getText());
optionsSet.add(option.getText());
}
// compare the two
Assert.assertEquals("The List contains duplicates!", optionsSet.size(), optionsList.size());
A modified version of this. If the array of String contains duplicates it will simply return true.
String[] array = {"a", "b", "c", "a", "b"};
ArrayList<String> str = new ArrayList<String>();
for (String s:array){
str.add(s);
}
boolean ind = false;
for (int i = 0; i < array.length; i++) {
str.remove(array[i]);
for (int j = 0; j < str.size(); j++) {
if (array[j].equals(str.get(j))){
System.out.println(str.get(j) +" "+ array[j] );
ind = true;
}
}
}
Note:This probably has nothing to do with Selenium, at least the way you showed in the question.
I'm looking to create an array that will be able to change size over time because the size of the array is unpredictable and I don't want to create a huge random number that will waste memory so every time a button is pressed I need the array to grow by one.
private String[][] lyricLineInfo = new String[x][5];
In the place of x is where the array must grow upon the button push and 5 is a constant. So I need the x button to grow by one without overflowing. Can I do it by using something like this?
lyricLineInfo[lyricLineInfo.length + 1][4] = fieldLyrics.getText();
Anyways thanks in advance!
Use an ArrayList<String[]> (see the docs here). It will grow automatically. (It uses an internal array that doesn't actually grow by just 1 when it needs to grow. Since growing is an expensive operation, it grows by some larger amount so it can absorb a few more items before having to grow again.)
EDIT
For example, here's how you could recode the two lines of your original post:
private ArrayList<String[]> lyricLineInfo = new ArrayList<String[]>();
lyricLineInfo.add(fieldLyrics.getText());
The second line assumes that fieldLyrics.getText() returns a String[]. If I misunderstood your intent and it returns a String, then you could do the following:
String[] nextStrings = new String[5];
nextStrings[4] = fieldLyrics.getText();
lyricLineInfo.add(nextStrings);
If the second index isn't always 5 long, you can also have an ArrayList of ArrayLists:
private ArrayList<ArrayList<String>> lyricLineInfo
= new ArrayList<ArrayList<String>>();
Then you could lyricLineInfo.add(new ArrayList<String>()); to extend the array.
EDIT 2
#clankfan1 - In your comment, you asked how to do a particular operation. Let's say we're using the ArrayList<ArrayList<String>> structure. It would go something like this:
ArrayList<ArrayList<String>> lyricLineInfo = new ArrayList<ArrayList<String>>();
ArrayList<String> line = new ArrayList<String>();
line.add("true");
line.add("true");
line.add("0.0");
line.add("5.0");
line.add("First Line");
lyricLineInfo.add(line);
line = new ArrayList<String>(); // don't use clear(): need a new object here
line.add("false");
line.add("false");
line.add("5.0");
line.add("10.0");
line.add("Second Line");
lyricLineInfo.add(line);
String secondLineTitle = lyricLineInfo.get(1).get(4); // will be "Second Line"
Obviously, this logic is amenable to being put into a separate method.
EDIT 3
If you need the elements of lyricLineInfo to be of type String[], it is vital that each element be a distinct array. Here are a few coding styles for adding elements:
ArrayList<String[]> lyricLineInfo = new ArrayList<String[]>();
String[] line = { "true", "true", "0.0", "5.0", "First Line" };
lyricLineInfo.add(line);
// now for a second style:
line = new String[5];
line[0] = "false";
line[1] = "false";
line[2] = "5.0";
line[3] = "10.0";
line[4] = "Second Line";
lyricLineInfo.add(line);
// and a third style:
lyricLineInfo.add(new String[] {
"false", "true", "10.0", "15.0", "Third Line"
});
String secondLineTitle = lyricLineInfo.get(1)[4]; // will be "Second Line"
You could use java.util.Vector<String[]>.
Use an ArrayList like this:
private ArrayList<ArrayList<String>> lyricLineInfo = new ArrayList<ArrayList<String>>();
ArrayLists are flexible arrays in java. When you want to add something do this:
lyricLineInfo.add(stringToBeAdded, index) //for the first dimension and
lyricLineInfo.get(firstIndex).add(stringToBeAdded, index); //for the second dimension
Use a List instead:
private List<String[]> lyricLineInfo = new ArrayList<String[]>();
Then to add to the list you use:
lyricLineInfo.add(new String[5]);
and to get you do:
// Get the 3rd element (array index 2).
String[] strings = lyricLineInfo.get(2);
Why don't you think about using Collections if you need an array with undefined size? :)
You can't do that with an array, but you can use a List object. You could try something like this:
private List<String[]> lyricLineInfo = new ArrayList<String[]>();
So then, assuming fieldLyrics.getText() returns a String[], you would do:
lyricLineInfo.add(fieldLyrics.getText());
If you only add new elements and iterate over all elements in list you should use LinkedList instead.
Collections are heavily used in Java. You should check Java Collections Framework - tutorial
I have created a Java code for my Android App.
String[] MovieName=new String[]{};
for (int i = 0; i < 15; i++)
{
MovieName[i]=movieAtt.getAttributeValue( "name" ); //Value coming from my XML
}
ListViewObject.setAdapter(new ArrayAdapter<String>(screen2.this,android.R.layout.simple_list_item_1 , MovieName));
This code throws an Exception.
I think i am not inserting vaues properly inside Java String Array.
All i want is to have a variable like MovieName={"1","2", "3"} to feed into the ListView of my code.
This is not much helpful too :
http://download.oracle.com/javase/6/docs/api/java/lang/String.html
You initialize an empty array.
Try this
String[] MovieName = new String[15];
Your initilizing an empty string array. That will give you an ArrayOutOfBoundException.
If you always have 15 entries you could initialize it to 15.
String[] MovieName=new String[15];
Otherwise you could create an ArrayList and convert it to an array after you filled it.
If number of elements in MovieName is constant, then you should initialise it as
String[] MovieName=new String[15];
Your current initialisation is equal to
String[] MovieName=new String[0];