List comparison and to get the Index [closed] - java

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 having 2 lists of object, one is UI object and another one is database object,i want to compare both the list of objects(UI & database object) and to get the index of UI Object's list.
List<ObjectVO> listOfVOObj = new ArrayList<ObjectVO>();
List<ObjectDB> listOfDBObj = new ArrayList<ObjectDB>();
ObjectVO{
private String regNo;
private String userId;
private String name;
...
}
ObjectDB{
private String regNo;
private String userId;
..
}
listOfVOObj:
index regNo userId name
1 123 456 name1
2 2233 567 name2
3 2234 568 name3
4 2235 569 name4
5 2236 570 name5
listOfDBObj:
index regNo userId
1 2233 567
2 2234 568
i have to compare both the list and want to get the index of the records in listOfVOObj which are matching to the records in listOfDBObj.

From your description it looks like this should be enough: (in the code I am comparing the objects on regNo)
List<string> indexList = new List<string>();
for (int i = 0; i < listOfVOObj.length(); i++)
{
for (int j = 0; j < listOfDBObj.length(); j++)
{
if (listOfVOObj[i].regNo == listOfDBObj[j].regNo && listOfVOObj[i].userId == listOfDBObj[j].userId)
{
int index = i + 1;
indexList.Add(index);
}
}
}
If you would like to store indexes of both objects first you have to create new class like this:
public class MyClass
{
string indexVOObj;
string indexDBObj;
public MyClass(string index1, string index2)
{
indexVOObj = index1;
indexDBObj = index2;
}
}
Then you have to use this:
List<MyClass> indexList = new List<MyClass>();
for (int i = 0; i < listOfVOObj.length(); i++)
{
for (int j = 0; j < listOfDBObj.length(); j++)
{
if (listOfVOObj[i].regNo == listOfDBObj[j].regNo && listOfVOObj[i].userId == listOfDBObj[j].userId)
{
int index1 = i + 1;
int index2 = j + 1;
indexList.Add(new MyClass(index1, index2));
}
}
}

Related

Creating a two dimensional string array [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 2 years ago.
Improve this question
For the question below use 2d array greetings seen below:
![image of 2D array][1]
"bob"
"joe"
"billy"
"george"
"janise"
"dell"
The code that I wrote is:
public class c
{
// instance variables - replace the example
below with your own
private int columns; int rows;
/**
* Constructor for objects of class c
*/
public c()
{
int columns = 2;
int rows = 3;
String[][] newArray = new String[columns][rows];
newArray[0][0] = "bob";
newArray[0][1] = "joe";
newArray[1][0] = "billy";
newArray[1][1] = "george";
newArray[1][2] = "janise";
newArray[2][2] = "dell";
for(int i = 0; i < rows; i++){
for(int j = 0; j < columns; j++){
System.out.println(newArray[i][j]);
}
}
}
}
When creating the array, you need to use new String[rows][columns], not new String[columns][rows].
The third row should then be assigned using newArray[2][0] and newArray[2][1].
You can use an array initializer to create the array in a much easier way:
String[][] newArray = { { "hello", "ni hao" },
{ "konnichiwa", "hola" },
{ "guten tag", "bonjour" } };
So... your code is close to being correct.
The first mistake is that you mixed the columns and rows: You are defining your array to have two columns, but in the end, you are using three (0, 1, 2).
The second mistake is in the last two assignments: You really did confuse the numbering here.
Your finished constructor should look like this:
int columns = 2;
int rows = 3;
String[][] newArray = new String[rows][columns];
newArray[0][0] = "hello";
newArray[0][1] = "ni hao";
newArray[1][0] = "konnichiwa";
newArray[1][1] = "hola";
newArray[2][0] = "guten tag";
newArray[2][1] = "bonjur";
for(int i = 0; i < rows; i++){
for(int j = 0; j < columns; j++){
System.out.println(newArray[i][j]);
}
}

iterate on sets of string in java [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
i have program that makes operations on string and this is the principal function of one of those operations,this works perfectly,but it is not efficient:
private void printString(ArrayList<String> operations, ArrayList<String> set) {
int numerOfStrings = 0;
int numberOfletters = 0;
String toPrint = operations.get(1);
outOfLoop: for (int i = 0; i < set.size(); i++) {
String[] toFind = set.get(i).split(" ");
for (int k = 0; k < toFind.length; k++) {
if (toPrint.equals(toFind[k])) {
String[] splited = set.get(i).split(" ");
for (int j = 0; j < splited.length; j++) {
numberOfletters += splited[j].length();
}
numerOfStrings = splited.length;
break outOfLoop;
}
}
}
System.out.println(numerOfStrings + " " + numberOfletters);
}
explanation:
this function takes as parameter a an arrayList of operations, and an arraylist of set:
for the arraylist of operations i get always a specific position so,i don't iterate , it is always O(1)
for the arrayList of set i have to iterate,or rather, as I think of that to proceed:
so , for example, if have as operation print foo i have to do these steps:
first of all i have to find where foo is :
inside set i can have this situation:
position 1 : {car tree hotel}
...
position n : {foo lemon coffee}
when i find the string foo i have to print the number of strings inside that position and the number of letters of each string,so in this case i will print :
3(number of strings) 14(number of letters)
my program works,and also this function works, but it is a nasty solution,and not efficent,what do you think, how can I improve in efficiency in my program?
question inserted on code Review
try this version:
private void printString(ArrayList<String> operations, ArrayList<String> set) {
int numerOfStrings = 0;
int numberOfletters = 0;
String toPrint = operations.get(1);
for (String s : set) {
if (s.contains(toPrint)) {
String[] arr = s.split(" ");
numerOfStrings = arr.length;
numberOfletters = s.length() - (numerOfStrings - 1);
break;
}
}
System.out.println(numerOfStrings + " " + numberOfletters);
}
The 'worst' case here would be O(n2), because of for loop O(n) and contains O(n).
hope it helps.

Insertion sort array 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 8 years ago.
Improve this question
So I have to write code for an insertion sort that will sort an array of random ints, the array is already set up and working okay and everything but my sort is not, heres what i have:
for(int i =1; i< numberSort.length-1;i++){
int temp = numberSort[i];
int j = i-1;
while((j >= 0) && (numberSort[j]>temp)){
numberSort[j+1] = numberSort[j];
j = j-1;
}
numberSort[j+1] = temp;
}
}
It seems to me that that should work, however it does not, it moves the numbers around from their original position but does not order them in ascending order. Thanks for any help you may be able to offer.
This code works for me:
public static void main(String[] args) {
int[] numberSort = {22,7,2, 5, 7, 1, 2, 9,33,55,12,1,0};
for (int i = 1; i < numberSort.length; i++) {
int temp = numberSort[i];
int j = i - 1;
while ((j >= 0) && (numberSort[j] > temp)) {
numberSort[j + 1] = numberSort[j];
j = j - 1;
}
numberSort[j + 1] = temp;
}
for (int i = 0; i < numberSort.length; i++) {
System.out.println(numberSort[i]);
}
}
Gives output:
0
1
1
2
2
5
7
7
9
12
22
33
55

Adding Elements in an array [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
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
Improve this question
array[0] = 1;
array[1] = 2;
array[2] = 3;
array[3] = 4;
items_arr = 4;
System.out.println("The elements in the array are: ");
for (int x = 0; x < items_arr; x++)
System.out.println("Array[" + x + "]=" + array[x]);
System.out.print("\n");
Scanner insert = new Scanner(System.in);
System.out.print("Enter an Element to Insert: ");
int input = insert.nextInt();
for (s = 0; s < items_arr; s++)
if (array[s] == input)
break;
items_arr++;
for (s = 0; s < items_arr; s++)
System.out.println("Array[" + s + "]=" + array[s]);
break;
The output is. The elements are
Array [0]= 1
Array [1]= 2
Array [2]= 3
Array [3]= 4
Enter an element to Insert: 5
Array [0]= 1
Array [1]= 2
Array [2]= 3
Array [3]= 4
Array [4]= 0
when I insert 5 it posts 0
any suggestions please.. thanks!
To insert in to the array you shuould be doing follwoing operation
array[s]=input
Two notes here
Arrays are fixed length, and you should be checking the array length before inserting values in to that,other wise you will get ArrayIndexOBException. Safer to sue List/Set
As better coding practise, and to improve the readablity, you should be enclosing the conditional/loop statements (such as if or for) - see eg below
eg: 1
for (int x = 0;x<items_arr;x++) {
System.out.println("Array["+x+"]="+array[x]);
}
eg 2:
for(int s = 0; s < items_arr; s++) {
if (array[s] == input) {
break;
}
}
You have not inserted 5 in your array,
do something after items_arr++
array[ items_arr] = input;
If you do not insert any thing then by default every element is 0
You should be using a Collection type; I would recommend an ArrayList - that is -
List<Integer> al = new ArrayList<Integer>();
for (int i = 1; i < 5; i++) {
al.add(i);
}
Scanner insert = new Scanner(System.in);
System.out.print("Enter an Element to Insert: ");
int input = insert.nextInt();
al.add(input); // And so on...
You are not updating/inserting the array with the new input.
for(s = 0; s < items_arr; s++)
if (array[s] == input)
break;
items_arr++;
just replace the above code with
array[ items_arr] = input;
items_arr++;

how do I compare the string in a1 to the string in a2 to see which is bigger? [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
Write a static method named joiner that accepts two arrays of strings a1 and a2 as parameters and returns a new array a3 such that each element of a3 at each index i stores a new string composed of the words stored at a1[i] and a2[i] joined together with the word with the work having greater length (more characters) at the front of the concatenated string. If there is a tie, take the element from a1.
For example, if a1 and a2 store the following elements:
String[] a1 = {"star", "pie", "jelly bean", "car"};
String[] a2 = {"cookie", "fig", "banana", "soda"};
Then your method should return the new array:
{"cookiestar", "piefig", "jelly beanbanana", "sodacar"}.
This is what i got so far:
for(int i = 0; i <length; i++) {
if(a1[i].stringlength()< a2[i].stringlength()) {
String[] a3 = a1[i] + a2[i];
}
else {
if(a2[i].stringlength().compareTo[i].stringlength() < 0) {
String[] a3 = a2[i] + a1[i];
}
else {
String[] a3 = a1[i] + a2[i];
}
return a3[i];
}
}
Alot of errors but i tried alot of different things and cant seem to figure it out
. I'm trying to figure out how to compare the strings in both arrays by the index. How would I compare a1[i] and a2[i] to see which string is bigger? And how would assign both strings to a new array a3?
The question does not specify if the two arrays of strings a1 and a2 are always the same length, and if not what to do when they are not, however with that in mind the following code works for when they are equal in length:
public static String[] joiner(String[] a1, String[] a2)
{
int length1 = a1.length;
int length2 = a2.length;
String[] a3 = new String[length1];
if( length1 == length2) {
for( int i = 0; i < length1; i++) {
if( a1[i].length() >= a2[i].length())
a3[i] = a1[i].concat(a2[i]);
else
a3[i] = a2[i].concat(a1[i]);
}
}
return a3;
}

Categories

Resources