Failure in ArrayStoreException - java
I have a question:
I have such a BDD table:
And the following set of "Dishes"
| Dish name |calories| quality | cost |
| grilled chicken| 400 | high | 12 |
| lasagna | 800 | low | 7 |
| gnocchi | 700 | high | 12 |
| pizza | 400 | low | 7 |
| snitzel | 400 | high | 12 |
And the following set of "Beverages"
| Beverage name | volume | quality | cost |
| coke | 35 | high | 5 |
| fanta | 35 | low | 2 |
| wine | 50 | high | 5 |
| beer | 50 | low | 2 |
| sprite | 35 | high | 5 |
and this is the implementation method os BDD data table.
public void the_following_set_of(String type, DataTable list) throws Throwable
{
if (type.equals("Dishes"))
{
List<List<String>> dishes = list.raw();
String[][] newdishes = new String[6][4];
newdishes.equals(dishes.toArray(newdishes));
for (int i = 1; i < 6; i++)
{
if (newdishes[i][2] == "high")
kalite.equals(Quality.high);
else
kalite.equals(Quality.low);
dishess.add(new Dish(newdishes[i][0], Integer.parseInt(newdishes[i][1]), kalite, Integer.parseInt(newdishes[i][3])));
}
}
if (type.equals("Beverages"))
{
List<List<String>> beverages = list.raw();
String[][] newbeverages = new String[6][4];
newbeverages = beverages.toArray(newbeverages);
for (int i = 1; i < 6; i++)
{
if (newbeverages[i][2] == "high")
kalite.equals(Quality.high);
else
kalite.equals(Quality.low);
beveragess.add(new Beverage(newbeverages[i][0], Integer.parseInt(newbeverages[i][1]), kalite, Integer.parseInt(newbeverages[i][3])));
}
}
restaurant.createMenu(dishess, beveragess);
}
I couldn't survive from ArrayStoreException here. Normally i'm trying to do cucumber feature testing, but when i run it, i encounter this problem.
When I run it eclipse says the problem is on this line:
newdishes.equals(dishes.toArray(newdishes));
How can i solve it?
Thanks.
Related
Count distinct while aggregating others?
This is how my dataset looks like: +---------+------------+-----------------+ | name |request_type| request_group_id| +---------+------------+-----------------+ |Michael | X | 1020 | |Michael | X | 1018 | |Joe | Y | 1018 | |Sam | X | 1018 | |Michael | Y | 1021 | |Sam | X | 1030 | |Elizabeth| Y | 1035 | +---------+------------+-----------------+ I want to calculate the amount of request_type's per person and count unique request_group_id's Result should be following: +---------+--------------------+---------------------+--------------------------------+ | name |cnt(request_type(X))| cnt(request_type(Y))| cnt(distinct(request_group_id))| +---------+--------------------+---------------------+--------------------------------+ |Michael | 2 | 1 | 3 | |Joe | 0 | 1 | 1 | |Sam | 2 | 0 | 2 | |John | 1 | 0 | 1 | |Elizabeth| 0 | 1 | 1 | +---------+--------------------+---------------------+--------------------------------+ What I've done so far: (helps to derive first two columns) msgDataFrame.select(NAME, REQUEST_TYPE) .groupBy(NAME) .pivot(REQUEST_TYPE, Lists.newArrayList(X, Y)) .agg(functions.count(REQUEST_TYPE)) .show(); How to count distinct request_group_id's in this select? Is it possible to do within it? I think it's possible only via two datasets join (my current result + separate aggregation by distinct request_group_id)
Example with "countDistinct" ("countDistinct" is not worked over window, replaced with "size","collect_set"): val groupIdWindow = Window.partitionBy("name") df.select($"name", $"request_type", size(collect_set("request_group_id").over(groupIdWindow)).alias("countDistinct")) .groupBy("name", "countDistinct") .pivot($"request_type", Seq("X", "Y")) .agg(count("request_type")) .show(false)
Issue with tracing down the array in Java recursion function
I have an issue with recursion in Java. The question is as such: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given n = 3, a solution set is: The code for the above problem is recursive and is as mentioned below: public List<String> generateParenthesis(int n) { ArrayList<String> result = new ArrayList<String>(); dfs(result, "", n, n); return result; } public void dfs(ArrayList<String> result, String s, int left, int right){ if(left > right) return; if(left==0&&right==0){ result.add(s); return; } if(left>0){ dfs(result, s+"(", left-1, right); } if(right>0){ dfs(result, s+")", left, right-1); } } I have been able to trace the program upto a particular point, but I am unable to trace it down totally. if n=2 left=2;right=2; result="(())", __________ | s="" | | l=2 | | r=2 | | | | | |________| | V __________ | s=( | | l 1 | | r 2 | | | | | |________| | V __________ | s=(( | | l 0 | | r 2 | | | | | |________| | V __________ | s=(() | | l 0 | | r 1 | | | | | |________| | V __________ | s= (())| | l=0 | | r=0 | | | | | |________| how would the program work after what I have mentioned above? Can someone help me tracing it? Thanks.
From where you left off: __________ | s=( | | l=1 | | r=2 | | | | | |________| | V __________ | s=() | | l 1 | | r 1 | | | | | |________| | V __________ | s=()( | | l 0 | | r 1 | | | | | |________| | V __________ | s=()() | | l 0 | | r 0 | | | | | |________| If you're using eclipse or any other IDE, it should be easy to set a breakpoint and go through how your program runs line by line (showing all your variables and how they change). If you haven't learned debugging yet, I would encourage you to google it and learn how to debug programs. What your program is actually doing: left (l=1, r=2) left (l=0, r=2) right (l=0, r=1) right (l=0, r=0) add result to s (l=0, r=0) *here you break out of 3 recursive functions and values of l,r reset to (l=1, r=2)* right (l=1, r=1) left (l=0, r=1) right (l=0, r=0) add result to s (l=0, r=0)
How to spread bits in a byte?
In Java, I have 2 bytes: byte b1 = (byte) 0b11111111, b2 = (byte) 0b00000000; I want to mix them so that every first bit is from b1 while the other is from b2 (reading left to right). The first and second halves of the inputs are done separate so the result is 2 bytes. The result b3 and b4 would look like the following. byte b3 = (byte) 0b10101010, b4 = 0b10101010; To illustrate how the bits are unique (using a letter to specify unique bit): byte b1 = (byte) 0bHGFEDCBA, b2 = (byte) 0bPONMLKJI; The result would be: byte b3 = (byte) 0bHPGOFNEM, b4 = 0bDLCKBJAI; Or, graphically, +---+---+---+---+---+---+---+---+ b1 | H | G | F | E | D | C | B | A | +---+---+---+---+---+---+---+---+ | | | | | | | | | | | | | | | +--------------------------------------------+ | | | | | | +----------------------------------------+ | | | | | | +------------------------------------+ | | | | | | +--------------------------------+ | | | | | | +-------------------+ | | | | | | +---------------+ | | | | | | +-----------+ | | | | | | +-------+ | | | | | | | | | | | | | | | +---+---+---+---+---+---+---+---+ +---+---+---+---+---+---+---+---+ b3 | H | P | G | O | F | N | E | M | b4 | D | L | C | K | B | J | A | I | +---+---+---+---+---+---+---+---+ +---+---+---+---+---+---+---+---+ | | | | | | | | +-----------+ | | | | | | | | +---------------+ | | | | | | | | +-------------------+ | | | | | | | | +-----------------------+ | | | | | | | | +------------------------------------+ | | | | | | | | +----------------------------------------+ | | | | | | | | +--------------------------------------------+ | | | | | | | | +------------------------------------------------+ | | | | | | | | +---+---+---+---+---+---+---+---+ b2 | P | O | N | M | L | K | J | I | +---+---+---+---+---+---+---+---+ What would be the simplest way to achieve this?
If, as you say, you had your heart set on a one-liner, how about: public static int interleave(short b1, short b2) { return((int)(((b2 * 0x0101010101010101L & 0x8040201008040201L) * 0x0102040810204081L >> 49) & 0x5555) | (int)(((b1 * 0x0101010101010101L & 0x8040201008040201L) * 0x0102040810204081L >> 48) & 0xAAAA)); } This will return an int with b3 & b4 as the lower 16 bits which you can shift and mask out: int b3b4 = interleave(b1, b2); int b3 = b3b4 >> 8; int b4 = b3b4 & 0b11111111; Algorithm courtesy of Interleave bits with 64-bit multiply
First, make a method that spreads bits in a byte, and returns an int with the bottom 16 bits set to the bits of the original byte: static int spread(int b) { int res = 0; for (int i = 0 ; i != 8 ; i++) { if ((b & 1<<i) != 0) { res |= 1<<(2*i); } } return res; } With this method in hand, produce the result by OR-ing the result of the first spread with the result of the second spread shifted over by one to the left: int res = spread(b1) | (spread(b2) << 1); Since your numbers are small, you can precompute spread(x) for all 256 possibilities. This produces Morton's table. Copy it into your class, and make your solution a one-liner: int res = morton[b1] | (morton[b2] << 1); // This declaration goes at the bottom of your file. // The numbers are copied from the program output at the link above: private static final short[] morton = new short[] { 0x0000, 0x0001, 0x0004, 0x0005, 0x0010, 0x0011, 0x0014, 0x0015, 0x0040, 0x0041, 0x0044, 0x0045, 0x0050, 0x0051, 0x0054, 0x0055, 0x0100, 0x0101, 0x0104, 0x0105, 0x0110, 0x0111, 0x0114, 0x0115, 0x0140, 0x0141, 0x0144, 0x0145, 0x0150, 0x0151, 0x0154, 0x0155, 0x0400, 0x0401, 0x0404, 0x0405, 0x0410, 0x0411, 0x0414, 0x0415, 0x0440, 0x0441, 0x0444, 0x0445, 0x0450, 0x0451, 0x0454, 0x0455, 0x0500, 0x0501, 0x0504, 0x0505, 0x0510, 0x0511, 0x0514, 0x0515, 0x0540, 0x0541, 0x0544, 0x0545, 0x0550, 0x0551, 0x0554, 0x0555, 0x1000, 0x1001, 0x1004, 0x1005, 0x1010, 0x1011, 0x1014, 0x1015, 0x1040, 0x1041, 0x1044, 0x1045, 0x1050, 0x1051, 0x1054, 0x1055, 0x1100, 0x1101, 0x1104, 0x1105, 0x1110, 0x1111, 0x1114, 0x1115, 0x1140, 0x1141, 0x1144, 0x1145, 0x1150, 0x1151, 0x1154, 0x1155, 0x1400, 0x1401, 0x1404, 0x1405, 0x1410, 0x1411, 0x1414, 0x1415, 0x1440, 0x1441, 0x1444, 0x1445, 0x1450, 0x1451, 0x1454, 0x1455, 0x1500, 0x1501, 0x1504, 0x1505, 0x1510, 0x1511, 0x1514, 0x1515, 0x1540, 0x1541, 0x1544, 0x1545, 0x1550, 0x1551, 0x1554, 0x1555, 0x4000, 0x4001, 0x4004, 0x4005, 0x4010, 0x4011, 0x4014, 0x4015, 0x4040, 0x4041, 0x4044, 0x4045, 0x4050, 0x4051, 0x4054, 0x4055, 0x4100, 0x4101, 0x4104, 0x4105, 0x4110, 0x4111, 0x4114, 0x4115, 0x4140, 0x4141, 0x4144, 0x4145, 0x4150, 0x4151, 0x4154, 0x4155, 0x4400, 0x4401, 0x4404, 0x4405, 0x4410, 0x4411, 0x4414, 0x4415, 0x4440, 0x4441, 0x4444, 0x4445, 0x4450, 0x4451, 0x4454, 0x4455, 0x4500, 0x4501, 0x4504, 0x4505, 0x4510, 0x4511, 0x4514, 0x4515, 0x4540, 0x4541, 0x4544, 0x4545, 0x4550, 0x4551, 0x4554, 0x4555, 0x5000, 0x5001, 0x5004, 0x5005, 0x5010, 0x5011, 0x5014, 0x5015, 0x5040, 0x5041, 0x5044, 0x5045, 0x5050, 0x5051, 0x5054, 0x5055, 0x5100, 0x5101, 0x5104, 0x5105, 0x5110, 0x5111, 0x5114, 0x5115, 0x5140, 0x5141, 0x5144, 0x5145, 0x5150, 0x5151, 0x5154, 0x5155, 0x5400, 0x5401, 0x5404, 0x5405, 0x5410, 0x5411, 0x5414, 0x5415, 0x5440, 0x5441, 0x5444, 0x5445, 0x5450, 0x5451, 0x5454, 0x5455, 0x5500, 0x5501, 0x5504, 0x5505, 0x5510, 0x5511, 0x5514, 0x5515, 0x5540, 0x5541, 0x5544, 0x5545, 0x5550, 0x5551, 0x5554, 0x5555 };
How to resolve Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2?
Can somebody help me solving this type of error Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2 I am searching for data in linked list but when I want to insert the data into an array, it turn up to be like this: matric | nama | sem | cc | ch | fm 32255 | izzat | 1 | ccs2 | 3 | 45.0 | | 2 | ccs3 | 3 | 56.0 32345 | khai] | 3 | ccs4 | 3 | 45.0 | | 2 | ccs5 | 3 | 2.0 32246 | fifi | 1 | cc1 | 3 | 60.0 | | 1 | ccs3 | 4 | 34.0 34567 | dudu | 2 | ccs2 | 2 | 24.0 | | 2 | ccs4 | 6 | 79.0 first-->34567-->32246-->32345-->32255-->null first-->6-->2-->4-->3-->3-->3-->3-->3-->null first-->2-->2-->1-->1-->2-->3-->2-->1-->null first-->dudu-->fifi-->khai]-->izzat-->null first-->ccs4-->ccs2-->ccs3-->cc1-->ccs5-->ccs4-->ccs3-->ccs2-->null first-->79.0-->24.0-->34.0-->60.0-->2.0-->45.0-->56.0-->45.0-->null 42insert matric= 032345 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2 2 khai] 2 3 at inputoutput.LinkedList.getcc(LinkedList.java:141) at inputoutput.baca.getcc(baca.java:84) at inputoutput.Inputoutput.main(Inputoutput.java:75) Java Result: 1 BUILD SUCCESSFUL (total time: 7 seconds) the code: String[] getcc(int mat,int sub) { ListObject2 current = first2; int count=0; String b[]=new String[2] ;//2 is the subject number==sub int x=0; while (current!=null ) { if(count==((mat*sub)+x) && ((mat*sub)+0)<((mat*sub)+x)<<((mat*sub)+sub)){ b[x]=current.data2; x++; } current=current.next; count++; } return b; } but I will get the input if search for last data in the linked list which is 032255 this is the output: matric | nama | sem | cc | ch | fm 32255 | izzat | 1 | ccs2 | 3 | 45.0 | | 2 | ccs3 | 3 | 56.0 32345 | khai] | 3 | ccs4 | 3 | 45.0 | | 2 | ccs5 | 3 | 2.0 32246 | fifi | 1 | cc1 | 3 | 60.0 | | 1 | ccs3 | 4 | 34.0 34567 | dudu | 2 | ccs2 | 2 | 24.0 | | 2 | ccs4 | 6 | 79.0 first-->34567-->32246-->32345-->32255-->null first-->6-->2-->4-->3-->3-->3-->3-->3-->null first-->2-->2-->1-->1-->2-->3-->2-->1-->null first-->dudu-->fifi-->khai]-->izzat-->null first-->ccs4-->ccs2-->ccs3-->cc1-->ccs5-->ccs4-->ccs3-->ccs2-->null first-->79.0-->24.0-->34.0-->60.0-->2.0-->45.0-->56.0-->45.0-->null 42insert matric= 032255 3 izzat 2 1 ccs3//the data i want to search ccs2//
You're going into the if statement more than twice while walking the list. If you do that, you'll go past the bounds of the b array (which can only hold two values). You should use an ArrayList instead so you can add as many items as you need.
how to get communicate with 3 tables
Hi I have a table parent and its fields are mysql> select * from parent; +----+----------+------------+-----------------------------------+---------+------+ | id | category | is_deleted | name | version | cid | +----+----------+------------+-----------------------------------+---------+------+ | 1 | default | | Front Office | 0 | NULL | | 2 | default | | Food And Beverage | 0 | NULL | | 3 | default | | House Keeping | 0 | NULL | | 4 | default | | General | 0 | NULL | | 5 | client | | SPA | 0 | NULL | | 7 | client | | house | 0 | NULL | | 8 | client | | test | 0 | NULL | | 9 | client | | ggg | 0 | 1 | | 10 | client | | dddd | 0 | 1 | | 11 | client | | test1 | 0 | 1 | | 12 | client | | java | 0 | 1 | | 13 | client | | dcfdcddd | 0 | 1 | | 14 | client | | qqqq | 0 | 1 | | 15 | client | | nnnnnn | 0 | 1 | | 16 | client | | category | 0 | 1 | | 17 | client | | sukant | 0 | 1 | | 18 | client | | bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb | 0 | 1 | I have another table parent_question mysql> select * from parent_question; +----+------------+---------+-----+------+ | id | is_deleted | version | pid | qid | +----+------------+---------+-----+------+ | 1 | | 0 | 1 | 1 | | 2 | | 0 | 1 | 2 | | 3 | | 0 | 1 | 3 | | 4 | | 0 | 1 | 4 | | 5 | | 0 | 1 | 5 | | 6 | | 0 | 1 | 6 | | 7 | | 0 | 2 | 7 | | 8 | | 0 | 2 | 1 | | 9 | | 0 | 2 | 2 | | 10 | | 0 | 2 | 8 | | 11 | | 0 | 3 | 9 | | 12 | | 0 | 3 | 1 | | 13 | | 0 | 3 | 10 | | 14 | | 0 | 3 | 11 | | 15 | | 0 | 4 | 12 | | 16 | | 0 | 1 | 1 | | 17 | | 0 | 1 | 2 | | 18 | | 0 | 1 | 3 | | 19 | | 0 | 5 | 13 | | 20 | | 0 | 2 | 7 | | 21 | | 0 | 2 | 2 | | 22 | | 0 | 1 | 14 | | 23 | | 0 | 1 | 15 | | 24 | | 0 | 1 | 16 | | 25 | | 1 | 1 | 17 | | 26 | | 0 | 1 | 21 | | 27 | | 0 | 2 | 22 | | 28 | | 0 | 13 | 23 | | 29 | | 0 | 9 | 24 | | 30 | | 0 | 12 | 25 | | 31 | | 0 | 12 | 26 | | 32 | | 0 | 12 | 27 | | 33 | | 0 | 12 | 28 | | 34 | | 0 | 14 | 29 | | 35 | | 0 | 15 | 30 | | 36 | | 0 | 10 | 31 | | 37 | | 0 | 4 | 32 | | 38 | | 0 | 16 | 33 | | 39 | | 0 | 10 | 34 | | 40 | | 0 | 3 | 35 | | 41 | | 0 | 17 | 36 | | 42 | | 0 | 1 | 37 | | 43 | | 0 | 1 | 38 | | 44 | | 0 | 18 | 39 | | 45 | | 0 | 18 | 40 | +----+------------+---------+-----+------+ 45 rows in set (0.00 sec) and this is my question table ysql> select * from question; ----+----------+------------+------------------------------------------------------------+-- id | category | is_deleted | question | v ----+----------+------------+------------------------------------------------------------+-- 1 | default | | Staff Courtesy | 2 | default | | Staff Response | 3 | default | | Check In | 4 | default | | Check Out | 5 | default | | Travel Desk | 6 | default | | Door Man | 7 | default | | Restaurant Ambiance | 8 | default | | Quality Of Food | 9 | default | | Cleanliness Of The Room | 10 | default | | Room Size | 11 | default | | Room Amenities | 12 | default | | Any Other Comments ? | 13 | client | | How is Food? | 14 | client | | test question | 15 | client | | test1 | 16 | client | | test2 | 17 | client | | test2 | 18 | client | | test2 | 19 | client | | working | 20 | client | | sss | 21 | client | | ggggg | 22 | client | | this is new question | 23 | client | | dddddddddddd | 24 | client | | ggggggggggggggggg | 25 | client | | what is a class? | 26 | client | | what is inheritance | 27 | client | | what is an object | 28 | client | | what is an abstract class? | 29 | client | | qqqq | 30 | client | | nnnn question | 31 | client | | add some | 32 | client | | general question | 33 | client | | category question | 34 | client | | hhhhhhhh | 35 | client | | this is hos | 36 | client | | gggg | 37 | client | | dddd | 38 | client | | ddddd | 39 | client | | bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb | 40 | client | | ggg | ----+----------+------------+------------------------------------------------------------+-- what I know I have pid of parent_question table; What I want question of question table; for example.If I were given to find the question whose pid is 18. So from the parent_question table I can know qid is 39 and 40 and from the question table 39 refers to bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb and 40 refers to ggg What i tried String queryString="SELECT distinct q FROM Question q , ParentQuestion pq ,Parent p where pq.qid.id = q.id and p.id = pq.pid.id and p.category = 'default' AND p.id = "+pid; Query query=entityManagerUtil.getQuery(queryString); List questionsList = query.getResultList(); return questionsList; but it did not work.I mean I get nothing in the list.Can anybody point out my mistake. question entity class import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Version; import javax.validation.constraints.Size; import org.springframework.beans.factory.annotation.Configurable; #Configurable #Entity public class Question { #Id #GeneratedValue(strategy = GenerationType.AUTO) #Column(name = "id") private Long id; #Version #Column(name = "version") private Integer version; private String question; private String category; private boolean isDeleted; public boolean isDeleted() { return isDeleted; } public void setDeleted(boolean isDeleted) { this.isDeleted = isDeleted; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public Integer getVersion() { return version; } public void setVersion(Integer version) { this.version = version; } public String getQuestion() { return question; } public void setQuestion(String question) { this.question = question; } public String getCategory() { return category; } public void setCategory(String category) { this.category = category; } } parent entity class import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.ManyToOne; import javax.persistence.Version; import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; import org.springframework.beans.factory.annotation.Configurable; #Configurable #Entity public class Parent { #Id #GeneratedValue(strategy = GenerationType.AUTO) #Column(name = "id") private Long id; #Version #Column(name = "version") private Integer version; #ManyToOne private Client cid; public Client getCid() { return cid; } public void setCid(Client cid) { this.cid = cid; } private boolean isDeleted; /* #ManyToOne private Client cid; public Client getCid() { return cid; } public void setCid(Client cid) { this.cid = cid; }*/ public boolean isDeleted() { return isDeleted; } public void setDeleted(boolean isDeleted) { this.isDeleted = isDeleted; } private String name; private String category; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public Integer getVersion() { return version; } public void setVersion(Integer version) { this.version = version; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getCategory() { return category; } public void setCategory(String category) { this.category = category; } }
Commenters are confused by your classes, and this is a good indication that you might need to think about your design. suninsky has a good point that you may not need to have an entity class call ParentQuestion (unless ParentQuestion has extra data about the relationship, of course). Here are some typical questions I would be asking. Does every Question have a Parent? If so then there should probably be a parent property on your Question class, mapped as #ManyToOne Does every a Parent object have a set of Questions? If yes, then the Parent object should probably have a property named questions, the type of which is some kind of collection of Question objects.