Taking values from multiple arraylist in order [closed] - java

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 9 years ago.
Improve this question
I have 4 array list
ArrayList<String[]> split_url = new ArrayList<String[]>();
ArrayList<String> mediaID = new ArrayList<String>();
ArrayList<String> productID = new ArrayList<String>();
ArrayList<String> clusResult = new ArrayList<String>();
and each arraylist contains some values. i want to take first value from each arralist and save that into one row in database then take second values from each database and save that to next row and so on.... how can i do that ?

1) first initialize all the array lists with zeros upto a certain length l.
2) add some values to each array list.
3) run the a loop like this,
for(int x=0;x<l;x++)
{
String a=split_url(x);
String b=mediaId(x);
String c=productId(x);
String d=clusResult(x);
// now store a,b,c,d as a record in your database;
}

Related

How to find the Index of string in java array from the given string value [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 9 months ago.
Improve this question
creditTIDstatusArray=[93312263-1-09722612223, 99802001-1-09102842369, 99802002-1-09102842369];
creditTIDstatusList.addAll(Arrays.asList(creditTIDstatusArry));
searchValue="99802002-1".
int retval=creditTIDstatusList.indexOf("99802002-1");
System.out.println("The element at index is:" retval);
Output: 2
Please let me know how I can find the index of the given above(searchValue)element.
As you never have posted the reproducible code.
Assumptions
creditTIDstatusArray is a String type array.
Your search query always stays in front of each String value in the array.
Multiple indexes may start with the same search value.
String[] creditTIDstatusArray=new String[]{"93312263-1-09722612223", "99802001-1-09102842369", "99802002-1-09102842369"};
String searchValue="99802002-1";
for (int i = 0; i < creditTIDstatusArray.length; i++) {
if(creditTIDstatusArray[i].startsWith(searchValue)){
System.out.println("Index :" + I); // this will print all the indexes that starts with searchvalue
}
}
I am not sure why you have added the array into a list and then searched index of because it will never work as you are searching only part of a String rather than the whole value or object.

read numbers and strings separated by comma from file (java) [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 2 years ago.
Improve this question
i have file
Which contains numbers and strings in this form
1,2,ssh,4,5
sor,7,8,bbt,10
How can I read the numbers/string one by one in a loop?
I tried to read like that:
input = new Scanner(data_to_insert);
String data;
while (input.hasNext()) {
data = input.next();
data = data.replaceAll(",", "");
println("data to hash ->" + data);
But I get for example:
data to hash ->12345
And I wanted to get first 1 after that 2 and so on
like this:
data to hash ->1
data to hash ->2
.
.
.
data to hash ->5
Use a for loop in your while loop to loop through everything in each line (and String.split(","); to separate by the commas. In your example code, you printed out each line instead of each comma-separated token.
input = new Scanner(data_to_insert);
String data;
while (input.hasNext()) {
String[] words = input.next().split(",");
for (String word : words) System.out.println("data to hash -> " + data);
}
This should work.

how to remove multiple characters by their indice from a string in java? [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
How can I remove multiple characters by their index from a string. I thought to use StringBuilder's deleteCharAt function. But if I delete char one by one, I will not keep track the right index.
For example :
String test = "0123456789"
int[] removingIndice = int[] {2, 4, 0};
// remove character from string
// result = "1356789"
Create a new string builder,
iterate on string, add elements to builder if its index not in the array
StringBuilder sb = new StringBuilder("");
for(int i = 0; i< test.length; i++){
if( !ArrayUtils.contains(removingIndice, i))
{
sb.append(test.charAt(i));
}
}
test = sb.toString();
String is immutable in Java, so you will need to create a new String with characters at positions 0,2,4 removed. As one option, you may use StringBuilder for that, as answered here: How to remove single character from a String
I think, you need to redesign the task:
"new string must contains all characters except ..."
Now, seems weak initial data structure and the goal.

All combinations of ArrayList<ArrayList<String>> Lists in JAVA [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 6 years ago.
Improve this question
I need help with a implementation of a function that gives me all combinations of ArrayList<ArrayList<String>> (not List<List<String>>) List in fix order.
An Example for what I need:
From:
List1:[1,2]
List2:[3,4]
List3:[5,6]
To:
String 1 "1,3,5"
String 2 "1,3,6"
String 3 "1,4,5"
String 4 "1,4,6"
String 5 "2,3,5"
String 6 "2,3,6"
String 7 "2,4,5"
String 8 "2,4,6"
All Strings in ArrayList<String> Object
Please help me, I have no idea how to implement it.
PS: The size of ArrayList<ArrayList<String>> is variable.
PSS: ...and the size from the ArrayList lists are variable.
After little changes from the old rev it works now (ArrayIndexOutOfBoundsException and deleting to much on result):
void generate(ArrayList<ArrayList<String>> lists,
ArrayList<String> result, int index) {
if (index >= lists.size()) {
System.out.println(String.valueOf(result));
return;
}
ArrayList<String> list = lists.get(index);
for (int i = 0; i < list.size(); i++) {
result.add(list.get(i));
generate(lists, result, index + 1);
result.remove(result.size() - 1);
}
}
}

Need to randomize Array of Strings to be returned as both ques. and answer for jeopardy program [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 8 years ago.
Improve this question
String[] am = new String[10];
am[0] = "Sam";
am[1] = "Sam";
am[2] = "Sam";
am[3] = "Sam";
am[4] = "Sam";
am[5] = "Sam";
am[6] = "Sam";
am[7] = "Sam";
am[8] = "Sam";
am[9] = "Sam";
For a jeopardy game, random questions per GUI Jpanel button click
Approach 1: Shuffling the list
You can shuffle your list using:
List<String> list = Arrays.asList(am);
Collections.shuffle(list);
Even better is starting with an ArrayList, and adding your possibilites to that ArrayList, completely ignoring the String[] array
List<String> list = new ArrayList<String>();
list.add("value1");
list.add("value2");
...
Collections.shuffle(list);
You can then retreive an element from the shuffeled list using get(index), i.e. :
String random = list.get(0);
Approach 2: Randomizing the index
If you insist on using the String[]-array, you can do
String random = am[new Random().nextInt(am.length)]
This will create a Random-object, and generate a random integer ranging from 0 to the length of your array minus one. Once this integer is generated, it will be used as an index for to get an element from your String[]-array.
You can use the same approach on an ArrayList:
String random = list.get(new Random().nextInt(list.size));

Categories