Inserting a number into a Java array (2d)

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 2038 Views - Last Post: 28 October 2009 - 06:20 PM Rate Topic: -----

#1 javabrijon  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 27
  • Joined: 27-October 09

Inserting a number into a Java array (2d)

Posted 28 October 2009 - 12:58 AM

Im am trying to write a class that inserts a number at a random position (@ 1 index in each line of the 2d array) code i have so far is:

int[][] array = { {11, 22, 33};
						{44, 55, 66};
						{77, 88, 99} };

private void insert(int[][] num){
	  int inNum = 5;
	  int a = (int) (Math.random()*3+1);
	  for(int i = 0; i < num.length; i++){
		  for(int j = 0; j < num[i].length; j++){
				 num[a] = ......... [b]<=not sure[/b]
		  }
	 }
}
[instance].insert(array);


so that a possible out could be like:

11, 5, 33
5, 55, 66
77, 88, 5

Is This A Good Question/Topic? 0
  • +

Replies To: Inserting a number into a Java array (2d)

#2 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Inserting a number into a Java array (2d)

Posted 28 October 2009 - 01:34 AM

Clarification: So you are trying to come up with a random number, then adding that same number into a random element in each row of the Array?
Was This Post Helpful? 0
  • +
  • -

#3 javabrijon  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 27
  • Joined: 27-October 09

Re: Inserting a number into a Java array (2d)

Posted 28 October 2009 - 02:16 AM

insert "5" in each row @ random location.
Was This Post Helpful? 0
  • +
  • -

#4 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Inserting a number into a Java array (2d)

Posted 28 October 2009 - 02:20 AM

You will only need 1 loop size of the # of rows. Then create a 2nd random number through the size of each row. Then just add the First random number into the loop variable which is the row, into 2nd random number which is the element inside the row.

Make sense?
Was This Post Helpful? 0
  • +
  • -

#5 javabrijon  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 27
  • Joined: 27-October 09

Re: Inserting a number into a Java array (2d)

Posted 28 October 2009 - 02:29 AM

nope, so ...

for(int i =0; i < num.length; i++){
	num[i][random] = 5;  



sorry im lost
Was This Post Helpful? 0
  • +
  • -

#6 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Inserting a number into a Java array (2d)

Posted 28 October 2009 - 02:31 AM

num[i][random.nextInt(num[i].length)] = 5;
Was This Post Helpful? 0
  • +
  • -

#7 javabrijon  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 27
  • Joined: 27-October 09

Re: Inserting a number into a Java array (2d)

Posted 28 October 2009 - 02:47 AM

 private void insert(int[][] array){
	   
		int insert = (int) (Math.random() * 4 + 1);
		for(int i = 0; i < array.length; i++){
			//for(int j = 0; j < array[i].length; j++){
				//array[i][insert] = (int) 5;
				[b]array[i][ten.nextInt(array[i].length)] = 5;[/b] 
			//}
		}
		for(int j = 0; j < array.length; j++){
			System.out.print(array[j] + ", ");
		}
		
	}


this gives me an error msg for that line "unsolved compilation error"

Are you sure this inserts 5 at a random location for each line?
Was This Post Helpful? 0
  • +
  • -

#8 dolphin362  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 7
  • Joined: 27-October 09

Re: Inserting a number into a Java array (2d)

Posted 28 October 2009 - 03:09 AM

I got the solution:

Following is my Answer:

11,5,33
44,55,5
77,88,5

If you want the source code, please send an email to contact me.
Email:** Removed ** (Title: Require Source Code)
Was This Post Helpful? 0
  • +
  • -

#9 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Inserting a number into a Java array (2d)

Posted 28 October 2009 - 04:00 AM

My apologies had to run to the store. Looks like someone has an answer so still need help?

@Dolphin
You really should not include your email and ask people to email you, should help them with the code that they have instead of giving them your code that works. We help here, not give out Code.
Was This Post Helpful? 0
  • +
  • -

#10 javabrijon  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 27
  • Joined: 27-October 09

Re: Inserting a number into a Java array (2d)

Posted 28 October 2009 - 04:24 AM

lol she wanted $15 bucks out of me! for the code! lol

um, still having trouble. i have got this now:

public class insertten {

	private void insert5(int[][] array){
		
		int insert;
		for(int i = 0; i < array.length; i++){
			for(int j = 0; j < array[i].length; j++){
				insert = (int) (Math.random() * 4 + 1);
				array[i][random.nextInt(array[i].length)] = 5; 
			}
		}
		for(int n = 0; n < array.length; n++){
			for(int m = 0; m < array[m].length; m++){
				System.out.print(array[n] + ", ");
			}
		}
		
	}
	
	public static void main(String[] args) {
		{array}		
		insertten it = new insertten();
		it.insert5(array);
	}

}


Was This Post Helpful? 0
  • +
  • -

#11 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Inserting a number into a Java array (2d)

Posted 28 October 2009 - 04:27 AM

Alright, um i used random which is the class you need to reate a random object.

Random r = new Random(); r.nextInt(array[i].length) ect.

Post new problems? :)
Was This Post Helpful? 0
  • +
  • -

#12 javabrijon  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 27
  • Joined: 27-October 09

Re: Inserting a number into a Java array (2d)

Posted 28 October 2009 - 04:33 AM

lol this is the output i get know? (which is address i suspect?)
[I@47b480, [I@47b480, [I@47b480, [I@47b480, [I@bf216a, [I@bf216a, [I@bf216a, [I@bf216a, [I@2ac982, [I@2ac982, [I@2ac982, [I@2ac982, [I@1389e4, [I@1389e4, [I@1389e4, [I@1389e4, [I@c20e24, [I@c20e24, [I@c20e24, [I@c20e24, 


Was This Post Helpful? 0
  • +
  • -

#13 dolphin362  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 7
  • Joined: 27-October 09

Re: Inserting a number into a Java array (2d)

Posted 28 October 2009 - 04:34 AM

Yes, I have requested.
Welcome to contact me again.
Was This Post Helpful? 0
  • +
  • -

#14 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Inserting a number into a Java array (2d)

Posted 28 October 2009 - 04:36 AM

Yeah that is the memory address... if you are using a loop and cycling through it not sure why it is doing that when the numbers are still ints.. Hmm, guess imma have to load this into my Jcreator, been trying to get it without dooing it so. GImme a couple min(5 min kid woke up) will see what I can mettle with

This post has been edited by Fuzzyness: 28 October 2009 - 04:39 AM

Was This Post Helpful? 0
  • +
  • -

#15 javabrijon  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 27
  • Joined: 27-October 09

Re: Inserting a number into a Java array (2d)

Posted 28 October 2009 - 04:39 AM

View Postdolphin362, on 28 Oct, 2009 - 03:34 AM, said:

Yes, I have requested.
Welcome to contact me again.

sorry ..no thanks
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2